sys.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/export.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/reboot.h>
  11. #include <linux/prctl.h>
  12. #include <linux/highuid.h>
  13. #include <linux/fs.h>
  14. #include <linux/kmod.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/resource.h>
  17. #include <linux/kernel.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/capability.h>
  20. #include <linux/device.h>
  21. #include <linux/key.h>
  22. #include <linux/times.h>
  23. #include <linux/posix-timers.h>
  24. #include <linux/security.h>
  25. #include <linux/dcookies.h>
  26. #include <linux/suspend.h>
  27. #include <linux/tty.h>
  28. #include <linux/signal.h>
  29. #include <linux/cn_proc.h>
  30. #include <linux/getcpu.h>
  31. #include <linux/task_io_accounting_ops.h>
  32. #include <linux/seccomp.h>
  33. #include <linux/cpu.h>
  34. #include <linux/personality.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/fs_struct.h>
  37. #include <linux/file.h>
  38. #include <linux/mount.h>
  39. #include <linux/gfp.h>
  40. #include <linux/syscore_ops.h>
  41. #include <linux/version.h>
  42. #include <linux/ctype.h>
  43. #include <linux/compat.h>
  44. #include <linux/syscalls.h>
  45. #include <linux/kprobes.h>
  46. #include <linux/user_namespace.h>
  47. #include <linux/binfmts.h>
  48. #include <linux/sched.h>
  49. #include <linux/rcupdate.h>
  50. #include <linux/uidgid.h>
  51. #include <linux/cred.h>
  52. #include <linux/nospec.h>
  53. #include <linux/kmsg_dump.h>
  54. /* Move somewhere else to avoid recompiling? */
  55. #include <generated/utsrelease.h>
  56. #include <asm/uaccess.h>
  57. #include <asm/io.h>
  58. #include <asm/unistd.h>
  59. #ifndef SET_UNALIGN_CTL
  60. # define SET_UNALIGN_CTL(a, b) (-EINVAL)
  61. #endif
  62. #ifndef GET_UNALIGN_CTL
  63. # define GET_UNALIGN_CTL(a, b) (-EINVAL)
  64. #endif
  65. #ifndef SET_FPEMU_CTL
  66. # define SET_FPEMU_CTL(a, b) (-EINVAL)
  67. #endif
  68. #ifndef GET_FPEMU_CTL
  69. # define GET_FPEMU_CTL(a, b) (-EINVAL)
  70. #endif
  71. #ifndef SET_FPEXC_CTL
  72. # define SET_FPEXC_CTL(a, b) (-EINVAL)
  73. #endif
  74. #ifndef GET_FPEXC_CTL
  75. # define GET_FPEXC_CTL(a, b) (-EINVAL)
  76. #endif
  77. #ifndef GET_ENDIAN
  78. # define GET_ENDIAN(a, b) (-EINVAL)
  79. #endif
  80. #ifndef SET_ENDIAN
  81. # define SET_ENDIAN(a, b) (-EINVAL)
  82. #endif
  83. #ifndef GET_TSC_CTL
  84. # define GET_TSC_CTL(a) (-EINVAL)
  85. #endif
  86. #ifndef SET_TSC_CTL
  87. # define SET_TSC_CTL(a) (-EINVAL)
  88. #endif
  89. #ifndef MPX_ENABLE_MANAGEMENT
  90. # define MPX_ENABLE_MANAGEMENT() (-EINVAL)
  91. #endif
  92. #ifndef MPX_DISABLE_MANAGEMENT
  93. # define MPX_DISABLE_MANAGEMENT() (-EINVAL)
  94. #endif
  95. #ifndef GET_FP_MODE
  96. # define GET_FP_MODE(a) (-EINVAL)
  97. #endif
  98. #ifndef SET_FP_MODE
  99. # define SET_FP_MODE(a,b) (-EINVAL)
  100. #endif
  101. /*
  102. * this is where the system-wide overflow UID and GID are defined, for
  103. * architectures that now have 32-bit UID/GID but didn't in the past
  104. */
  105. int overflowuid = DEFAULT_OVERFLOWUID;
  106. int overflowgid = DEFAULT_OVERFLOWGID;
  107. EXPORT_SYMBOL(overflowuid);
  108. EXPORT_SYMBOL(overflowgid);
  109. /*
  110. * the same as above, but for filesystems which can only store a 16-bit
  111. * UID and GID. as such, this is needed on all architectures
  112. */
  113. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  114. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  115. EXPORT_SYMBOL(fs_overflowuid);
  116. EXPORT_SYMBOL(fs_overflowgid);
  117. /*
  118. * Returns true if current's euid is same as p's uid or euid,
  119. * or has CAP_SYS_NICE to p's user_ns.
  120. *
  121. * Called with rcu_read_lock, creds are safe
  122. */
  123. static bool set_one_prio_perm(struct task_struct *p)
  124. {
  125. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  126. if (uid_eq(pcred->uid, cred->euid) ||
  127. uid_eq(pcred->euid, cred->euid))
  128. return true;
  129. if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
  130. return true;
  131. return false;
  132. }
  133. /*
  134. * set the priority of a task
  135. * - the caller must hold the RCU read lock
  136. */
  137. static int set_one_prio(struct task_struct *p, int niceval, int error)
  138. {
  139. int no_nice;
  140. if (!set_one_prio_perm(p)) {
  141. error = -EPERM;
  142. goto out;
  143. }
  144. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  145. error = -EACCES;
  146. goto out;
  147. }
  148. no_nice = security_task_setnice(p, niceval);
  149. if (no_nice) {
  150. error = no_nice;
  151. goto out;
  152. }
  153. if (error == -ESRCH)
  154. error = 0;
  155. set_user_nice(p, niceval);
  156. out:
  157. return error;
  158. }
  159. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  160. {
  161. struct task_struct *g, *p;
  162. struct user_struct *user;
  163. const struct cred *cred = current_cred();
  164. int error = -EINVAL;
  165. struct pid *pgrp;
  166. kuid_t uid;
  167. if (which > PRIO_USER || which < PRIO_PROCESS)
  168. goto out;
  169. /* normalize: avoid signed division (rounding problems) */
  170. error = -ESRCH;
  171. if (niceval < MIN_NICE)
  172. niceval = MIN_NICE;
  173. if (niceval > MAX_NICE)
  174. niceval = MAX_NICE;
  175. rcu_read_lock();
  176. read_lock(&tasklist_lock);
  177. switch (which) {
  178. case PRIO_PROCESS:
  179. if (who)
  180. p = find_task_by_vpid(who);
  181. else
  182. p = current;
  183. if (p)
  184. error = set_one_prio(p, niceval, error);
  185. break;
  186. case PRIO_PGRP:
  187. if (who)
  188. pgrp = find_vpid(who);
  189. else
  190. pgrp = task_pgrp(current);
  191. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  192. error = set_one_prio(p, niceval, error);
  193. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  194. break;
  195. case PRIO_USER:
  196. uid = make_kuid(cred->user_ns, who);
  197. user = cred->user;
  198. if (!who)
  199. uid = cred->uid;
  200. else if (!uid_eq(uid, cred->uid)) {
  201. user = find_user(uid);
  202. if (!user)
  203. goto out_unlock; /* No processes for this user */
  204. }
  205. do_each_thread(g, p) {
  206. if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
  207. error = set_one_prio(p, niceval, error);
  208. } while_each_thread(g, p);
  209. if (!uid_eq(uid, cred->uid))
  210. free_uid(user); /* For find_user() */
  211. break;
  212. }
  213. out_unlock:
  214. read_unlock(&tasklist_lock);
  215. rcu_read_unlock();
  216. out:
  217. return error;
  218. }
  219. /*
  220. * Ugh. To avoid negative return values, "getpriority()" will
  221. * not return the normal nice-value, but a negated value that
  222. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  223. * to stay compatible.
  224. */
  225. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  226. {
  227. struct task_struct *g, *p;
  228. struct user_struct *user;
  229. const struct cred *cred = current_cred();
  230. long niceval, retval = -ESRCH;
  231. struct pid *pgrp;
  232. kuid_t uid;
  233. if (which > PRIO_USER || which < PRIO_PROCESS)
  234. return -EINVAL;
  235. rcu_read_lock();
  236. read_lock(&tasklist_lock);
  237. switch (which) {
  238. case PRIO_PROCESS:
  239. if (who)
  240. p = find_task_by_vpid(who);
  241. else
  242. p = current;
  243. if (p) {
  244. niceval = nice_to_rlimit(task_nice(p));
  245. if (niceval > retval)
  246. retval = niceval;
  247. }
  248. break;
  249. case PRIO_PGRP:
  250. if (who)
  251. pgrp = find_vpid(who);
  252. else
  253. pgrp = task_pgrp(current);
  254. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  255. niceval = nice_to_rlimit(task_nice(p));
  256. if (niceval > retval)
  257. retval = niceval;
  258. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  259. break;
  260. case PRIO_USER:
  261. uid = make_kuid(cred->user_ns, who);
  262. user = cred->user;
  263. if (!who)
  264. uid = cred->uid;
  265. else if (!uid_eq(uid, cred->uid)) {
  266. user = find_user(uid);
  267. if (!user)
  268. goto out_unlock; /* No processes for this user */
  269. }
  270. do_each_thread(g, p) {
  271. if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
  272. niceval = nice_to_rlimit(task_nice(p));
  273. if (niceval > retval)
  274. retval = niceval;
  275. }
  276. } while_each_thread(g, p);
  277. if (!uid_eq(uid, cred->uid))
  278. free_uid(user); /* for find_user() */
  279. break;
  280. }
  281. out_unlock:
  282. read_unlock(&tasklist_lock);
  283. rcu_read_unlock();
  284. return retval;
  285. }
  286. /*
  287. * Unprivileged users may change the real gid to the effective gid
  288. * or vice versa. (BSD-style)
  289. *
  290. * If you set the real gid at all, or set the effective gid to a value not
  291. * equal to the real gid, then the saved gid is set to the new effective gid.
  292. *
  293. * This makes it possible for a setgid program to completely drop its
  294. * privileges, which is often a useful assertion to make when you are doing
  295. * a security audit over a program.
  296. *
  297. * The general idea is that a program which uses just setregid() will be
  298. * 100% compatible with BSD. A program which uses just setgid() will be
  299. * 100% compatible with POSIX with saved IDs.
  300. *
  301. * SMP: There are not races, the GIDs are checked only by filesystem
  302. * operations (as far as semantic preservation is concerned).
  303. */
  304. #ifdef CONFIG_MULTIUSER
  305. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  306. {
  307. struct user_namespace *ns = current_user_ns();
  308. const struct cred *old;
  309. struct cred *new;
  310. int retval;
  311. kgid_t krgid, kegid;
  312. krgid = make_kgid(ns, rgid);
  313. kegid = make_kgid(ns, egid);
  314. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  315. return -EINVAL;
  316. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  317. return -EINVAL;
  318. new = prepare_creds();
  319. if (!new)
  320. return -ENOMEM;
  321. old = current_cred();
  322. retval = -EPERM;
  323. if (rgid != (gid_t) -1) {
  324. if (gid_eq(old->gid, krgid) ||
  325. gid_eq(old->egid, krgid) ||
  326. ns_capable(old->user_ns, CAP_SETGID))
  327. new->gid = krgid;
  328. else
  329. goto error;
  330. }
  331. if (egid != (gid_t) -1) {
  332. if (gid_eq(old->gid, kegid) ||
  333. gid_eq(old->egid, kegid) ||
  334. gid_eq(old->sgid, kegid) ||
  335. ns_capable(old->user_ns, CAP_SETGID))
  336. new->egid = kegid;
  337. else
  338. goto error;
  339. }
  340. if (rgid != (gid_t) -1 ||
  341. (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
  342. new->sgid = new->egid;
  343. new->fsgid = new->egid;
  344. return commit_creds(new);
  345. error:
  346. abort_creds(new);
  347. return retval;
  348. }
  349. /*
  350. * setgid() is implemented like SysV w/ SAVED_IDS
  351. *
  352. * SMP: Same implicit races as above.
  353. */
  354. SYSCALL_DEFINE1(setgid, gid_t, gid)
  355. {
  356. struct user_namespace *ns = current_user_ns();
  357. const struct cred *old;
  358. struct cred *new;
  359. int retval;
  360. kgid_t kgid;
  361. kgid = make_kgid(ns, gid);
  362. if (!gid_valid(kgid))
  363. return -EINVAL;
  364. new = prepare_creds();
  365. if (!new)
  366. return -ENOMEM;
  367. old = current_cred();
  368. retval = -EPERM;
  369. if (ns_capable(old->user_ns, CAP_SETGID))
  370. new->gid = new->egid = new->sgid = new->fsgid = kgid;
  371. else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
  372. new->egid = new->fsgid = kgid;
  373. else
  374. goto error;
  375. return commit_creds(new);
  376. error:
  377. abort_creds(new);
  378. return retval;
  379. }
  380. /*
  381. * change the user struct in a credentials set to match the new UID
  382. */
  383. static int set_user(struct cred *new)
  384. {
  385. struct user_struct *new_user;
  386. new_user = alloc_uid(new->uid);
  387. if (!new_user)
  388. return -EAGAIN;
  389. /*
  390. * We don't fail in case of NPROC limit excess here because too many
  391. * poorly written programs don't check set*uid() return code, assuming
  392. * it never fails if called by root. We may still enforce NPROC limit
  393. * for programs doing set*uid()+execve() by harmlessly deferring the
  394. * failure to the execve() stage.
  395. */
  396. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  397. new_user != INIT_USER)
  398. current->flags |= PF_NPROC_EXCEEDED;
  399. else
  400. current->flags &= ~PF_NPROC_EXCEEDED;
  401. free_uid(new->user);
  402. new->user = new_user;
  403. return 0;
  404. }
  405. /*
  406. * Unprivileged users may change the real uid to the effective uid
  407. * or vice versa. (BSD-style)
  408. *
  409. * If you set the real uid at all, or set the effective uid to a value not
  410. * equal to the real uid, then the saved uid is set to the new effective uid.
  411. *
  412. * This makes it possible for a setuid program to completely drop its
  413. * privileges, which is often a useful assertion to make when you are doing
  414. * a security audit over a program.
  415. *
  416. * The general idea is that a program which uses just setreuid() will be
  417. * 100% compatible with BSD. A program which uses just setuid() will be
  418. * 100% compatible with POSIX with saved IDs.
  419. */
  420. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  421. {
  422. struct user_namespace *ns = current_user_ns();
  423. const struct cred *old;
  424. struct cred *new;
  425. int retval;
  426. kuid_t kruid, keuid;
  427. kruid = make_kuid(ns, ruid);
  428. keuid = make_kuid(ns, euid);
  429. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  430. return -EINVAL;
  431. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  432. return -EINVAL;
  433. new = prepare_creds();
  434. if (!new)
  435. return -ENOMEM;
  436. old = current_cred();
  437. retval = -EPERM;
  438. if (ruid != (uid_t) -1) {
  439. new->uid = kruid;
  440. if (!uid_eq(old->uid, kruid) &&
  441. !uid_eq(old->euid, kruid) &&
  442. !ns_capable(old->user_ns, CAP_SETUID))
  443. goto error;
  444. }
  445. if (euid != (uid_t) -1) {
  446. new->euid = keuid;
  447. if (!uid_eq(old->uid, keuid) &&
  448. !uid_eq(old->euid, keuid) &&
  449. !uid_eq(old->suid, keuid) &&
  450. !ns_capable(old->user_ns, CAP_SETUID))
  451. goto error;
  452. }
  453. if (!uid_eq(new->uid, old->uid)) {
  454. retval = set_user(new);
  455. if (retval < 0)
  456. goto error;
  457. }
  458. if (ruid != (uid_t) -1 ||
  459. (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
  460. new->suid = new->euid;
  461. new->fsuid = new->euid;
  462. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  463. if (retval < 0)
  464. goto error;
  465. return commit_creds(new);
  466. error:
  467. abort_creds(new);
  468. return retval;
  469. }
  470. /*
  471. * setuid() is implemented like SysV with SAVED_IDS
  472. *
  473. * Note that SAVED_ID's is deficient in that a setuid root program
  474. * like sendmail, for example, cannot set its uid to be a normal
  475. * user and then switch back, because if you're root, setuid() sets
  476. * the saved uid too. If you don't like this, blame the bright people
  477. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  478. * will allow a root program to temporarily drop privileges and be able to
  479. * regain them by swapping the real and effective uid.
  480. */
  481. SYSCALL_DEFINE1(setuid, uid_t, uid)
  482. {
  483. struct user_namespace *ns = current_user_ns();
  484. const struct cred *old;
  485. struct cred *new;
  486. int retval;
  487. kuid_t kuid;
  488. kuid = make_kuid(ns, uid);
  489. if (!uid_valid(kuid))
  490. return -EINVAL;
  491. new = prepare_creds();
  492. if (!new)
  493. return -ENOMEM;
  494. old = current_cred();
  495. retval = -EPERM;
  496. if (ns_capable(old->user_ns, CAP_SETUID)) {
  497. new->suid = new->uid = kuid;
  498. if (!uid_eq(kuid, old->uid)) {
  499. retval = set_user(new);
  500. if (retval < 0)
  501. goto error;
  502. }
  503. } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
  504. goto error;
  505. }
  506. new->fsuid = new->euid = kuid;
  507. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  508. if (retval < 0)
  509. goto error;
  510. return commit_creds(new);
  511. error:
  512. abort_creds(new);
  513. return retval;
  514. }
  515. /*
  516. * This function implements a generic ability to update ruid, euid,
  517. * and suid. This allows you to implement the 4.4 compatible seteuid().
  518. */
  519. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  520. {
  521. struct user_namespace *ns = current_user_ns();
  522. const struct cred *old;
  523. struct cred *new;
  524. int retval;
  525. kuid_t kruid, keuid, ksuid;
  526. kruid = make_kuid(ns, ruid);
  527. keuid = make_kuid(ns, euid);
  528. ksuid = make_kuid(ns, suid);
  529. if ((ruid != (uid_t) -1) && !uid_valid(kruid))
  530. return -EINVAL;
  531. if ((euid != (uid_t) -1) && !uid_valid(keuid))
  532. return -EINVAL;
  533. if ((suid != (uid_t) -1) && !uid_valid(ksuid))
  534. return -EINVAL;
  535. new = prepare_creds();
  536. if (!new)
  537. return -ENOMEM;
  538. old = current_cred();
  539. retval = -EPERM;
  540. if (!ns_capable(old->user_ns, CAP_SETUID)) {
  541. if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
  542. !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
  543. goto error;
  544. if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
  545. !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
  546. goto error;
  547. if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
  548. !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
  549. goto error;
  550. }
  551. if (ruid != (uid_t) -1) {
  552. new->uid = kruid;
  553. if (!uid_eq(kruid, old->uid)) {
  554. retval = set_user(new);
  555. if (retval < 0)
  556. goto error;
  557. }
  558. }
  559. if (euid != (uid_t) -1)
  560. new->euid = keuid;
  561. if (suid != (uid_t) -1)
  562. new->suid = ksuid;
  563. new->fsuid = new->euid;
  564. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  565. if (retval < 0)
  566. goto error;
  567. return commit_creds(new);
  568. error:
  569. abort_creds(new);
  570. return retval;
  571. }
  572. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
  573. {
  574. const struct cred *cred = current_cred();
  575. int retval;
  576. uid_t ruid, euid, suid;
  577. ruid = from_kuid_munged(cred->user_ns, cred->uid);
  578. euid = from_kuid_munged(cred->user_ns, cred->euid);
  579. suid = from_kuid_munged(cred->user_ns, cred->suid);
  580. retval = put_user(ruid, ruidp);
  581. if (!retval) {
  582. retval = put_user(euid, euidp);
  583. if (!retval)
  584. return put_user(suid, suidp);
  585. }
  586. return retval;
  587. }
  588. /*
  589. * Same as above, but for rgid, egid, sgid.
  590. */
  591. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  592. {
  593. struct user_namespace *ns = current_user_ns();
  594. const struct cred *old;
  595. struct cred *new;
  596. int retval;
  597. kgid_t krgid, kegid, ksgid;
  598. krgid = make_kgid(ns, rgid);
  599. kegid = make_kgid(ns, egid);
  600. ksgid = make_kgid(ns, sgid);
  601. if ((rgid != (gid_t) -1) && !gid_valid(krgid))
  602. return -EINVAL;
  603. if ((egid != (gid_t) -1) && !gid_valid(kegid))
  604. return -EINVAL;
  605. if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
  606. return -EINVAL;
  607. new = prepare_creds();
  608. if (!new)
  609. return -ENOMEM;
  610. old = current_cred();
  611. retval = -EPERM;
  612. if (!ns_capable(old->user_ns, CAP_SETGID)) {
  613. if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
  614. !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
  615. goto error;
  616. if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
  617. !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
  618. goto error;
  619. if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
  620. !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
  621. goto error;
  622. }
  623. if (rgid != (gid_t) -1)
  624. new->gid = krgid;
  625. if (egid != (gid_t) -1)
  626. new->egid = kegid;
  627. if (sgid != (gid_t) -1)
  628. new->sgid = ksgid;
  629. new->fsgid = new->egid;
  630. return commit_creds(new);
  631. error:
  632. abort_creds(new);
  633. return retval;
  634. }
  635. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
  636. {
  637. const struct cred *cred = current_cred();
  638. int retval;
  639. gid_t rgid, egid, sgid;
  640. rgid = from_kgid_munged(cred->user_ns, cred->gid);
  641. egid = from_kgid_munged(cred->user_ns, cred->egid);
  642. sgid = from_kgid_munged(cred->user_ns, cred->sgid);
  643. retval = put_user(rgid, rgidp);
  644. if (!retval) {
  645. retval = put_user(egid, egidp);
  646. if (!retval)
  647. retval = put_user(sgid, sgidp);
  648. }
  649. return retval;
  650. }
  651. /*
  652. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  653. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  654. * whatever uid it wants to). It normally shadows "euid", except when
  655. * explicitly set by setfsuid() or for access..
  656. */
  657. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  658. {
  659. const struct cred *old;
  660. struct cred *new;
  661. uid_t old_fsuid;
  662. kuid_t kuid;
  663. old = current_cred();
  664. old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
  665. kuid = make_kuid(old->user_ns, uid);
  666. if (!uid_valid(kuid))
  667. return old_fsuid;
  668. new = prepare_creds();
  669. if (!new)
  670. return old_fsuid;
  671. if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
  672. uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
  673. ns_capable(old->user_ns, CAP_SETUID)) {
  674. if (!uid_eq(kuid, old->fsuid)) {
  675. new->fsuid = kuid;
  676. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  677. goto change_okay;
  678. }
  679. }
  680. abort_creds(new);
  681. return old_fsuid;
  682. change_okay:
  683. commit_creds(new);
  684. return old_fsuid;
  685. }
  686. /*
  687. * Samma på svenska..
  688. */
  689. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  690. {
  691. const struct cred *old;
  692. struct cred *new;
  693. gid_t old_fsgid;
  694. kgid_t kgid;
  695. old = current_cred();
  696. old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
  697. kgid = make_kgid(old->user_ns, gid);
  698. if (!gid_valid(kgid))
  699. return old_fsgid;
  700. new = prepare_creds();
  701. if (!new)
  702. return old_fsgid;
  703. if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
  704. gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
  705. ns_capable(old->user_ns, CAP_SETGID)) {
  706. if (!gid_eq(kgid, old->fsgid)) {
  707. new->fsgid = kgid;
  708. goto change_okay;
  709. }
  710. }
  711. abort_creds(new);
  712. return old_fsgid;
  713. change_okay:
  714. commit_creds(new);
  715. return old_fsgid;
  716. }
  717. #endif /* CONFIG_MULTIUSER */
  718. /**
  719. * sys_getpid - return the thread group id of the current process
  720. *
  721. * Note, despite the name, this returns the tgid not the pid. The tgid and
  722. * the pid are identical unless CLONE_THREAD was specified on clone() in
  723. * which case the tgid is the same in all threads of the same group.
  724. *
  725. * This is SMP safe as current->tgid does not change.
  726. */
  727. SYSCALL_DEFINE0(getpid)
  728. {
  729. return task_tgid_vnr(current);
  730. }
  731. /* Thread ID - the internal kernel "pid" */
  732. SYSCALL_DEFINE0(gettid)
  733. {
  734. return task_pid_vnr(current);
  735. }
  736. /*
  737. * Accessing ->real_parent is not SMP-safe, it could
  738. * change from under us. However, we can use a stale
  739. * value of ->real_parent under rcu_read_lock(), see
  740. * release_task()->call_rcu(delayed_put_task_struct).
  741. */
  742. SYSCALL_DEFINE0(getppid)
  743. {
  744. int pid;
  745. rcu_read_lock();
  746. pid = task_tgid_vnr(rcu_dereference(current->real_parent));
  747. rcu_read_unlock();
  748. return pid;
  749. }
  750. SYSCALL_DEFINE0(getuid)
  751. {
  752. /* Only we change this so SMP safe */
  753. return from_kuid_munged(current_user_ns(), current_uid());
  754. }
  755. SYSCALL_DEFINE0(geteuid)
  756. {
  757. /* Only we change this so SMP safe */
  758. return from_kuid_munged(current_user_ns(), current_euid());
  759. }
  760. SYSCALL_DEFINE0(getgid)
  761. {
  762. /* Only we change this so SMP safe */
  763. return from_kgid_munged(current_user_ns(), current_gid());
  764. }
  765. SYSCALL_DEFINE0(getegid)
  766. {
  767. /* Only we change this so SMP safe */
  768. return from_kgid_munged(current_user_ns(), current_egid());
  769. }
  770. void do_sys_times(struct tms *tms)
  771. {
  772. cputime_t tgutime, tgstime, cutime, cstime;
  773. thread_group_cputime_adjusted(current, &tgutime, &tgstime);
  774. cutime = current->signal->cutime;
  775. cstime = current->signal->cstime;
  776. tms->tms_utime = cputime_to_clock_t(tgutime);
  777. tms->tms_stime = cputime_to_clock_t(tgstime);
  778. tms->tms_cutime = cputime_to_clock_t(cutime);
  779. tms->tms_cstime = cputime_to_clock_t(cstime);
  780. }
  781. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  782. {
  783. if (tbuf) {
  784. struct tms tmp;
  785. do_sys_times(&tmp);
  786. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  787. return -EFAULT;
  788. }
  789. force_successful_syscall_return();
  790. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  791. }
  792. /*
  793. * This needs some heavy checking ...
  794. * I just haven't the stomach for it. I also don't fully
  795. * understand sessions/pgrp etc. Let somebody who does explain it.
  796. *
  797. * OK, I think I have the protection semantics right.... this is really
  798. * only important on a multi-user system anyway, to make sure one user
  799. * can't send a signal to a process owned by another. -TYT, 12/12/91
  800. *
  801. * !PF_FORKNOEXEC check to conform completely to POSIX.
  802. */
  803. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  804. {
  805. struct task_struct *p;
  806. struct task_struct *group_leader = current->group_leader;
  807. struct pid *pgrp;
  808. int err;
  809. if (!pid)
  810. pid = task_pid_vnr(group_leader);
  811. if (!pgid)
  812. pgid = pid;
  813. if (pgid < 0)
  814. return -EINVAL;
  815. rcu_read_lock();
  816. /* From this point forward we keep holding onto the tasklist lock
  817. * so that our parent does not change from under us. -DaveM
  818. */
  819. write_lock_irq(&tasklist_lock);
  820. err = -ESRCH;
  821. p = find_task_by_vpid(pid);
  822. if (!p)
  823. goto out;
  824. err = -EINVAL;
  825. if (!thread_group_leader(p))
  826. goto out;
  827. if (same_thread_group(p->real_parent, group_leader)) {
  828. err = -EPERM;
  829. if (task_session(p) != task_session(group_leader))
  830. goto out;
  831. err = -EACCES;
  832. if (!(p->flags & PF_FORKNOEXEC))
  833. goto out;
  834. } else {
  835. err = -ESRCH;
  836. if (p != group_leader)
  837. goto out;
  838. }
  839. err = -EPERM;
  840. if (p->signal->leader)
  841. goto out;
  842. pgrp = task_pid(p);
  843. if (pgid != pid) {
  844. struct task_struct *g;
  845. pgrp = find_vpid(pgid);
  846. g = pid_task(pgrp, PIDTYPE_PGID);
  847. if (!g || task_session(g) != task_session(group_leader))
  848. goto out;
  849. }
  850. err = security_task_setpgid(p, pgid);
  851. if (err)
  852. goto out;
  853. if (task_pgrp(p) != pgrp)
  854. change_pid(p, PIDTYPE_PGID, pgrp);
  855. err = 0;
  856. out:
  857. /* All paths lead to here, thus we are safe. -DaveM */
  858. write_unlock_irq(&tasklist_lock);
  859. rcu_read_unlock();
  860. return err;
  861. }
  862. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  863. {
  864. struct task_struct *p;
  865. struct pid *grp;
  866. int retval;
  867. rcu_read_lock();
  868. if (!pid)
  869. grp = task_pgrp(current);
  870. else {
  871. retval = -ESRCH;
  872. p = find_task_by_vpid(pid);
  873. if (!p)
  874. goto out;
  875. grp = task_pgrp(p);
  876. if (!grp)
  877. goto out;
  878. retval = security_task_getpgid(p);
  879. if (retval)
  880. goto out;
  881. }
  882. retval = pid_vnr(grp);
  883. out:
  884. rcu_read_unlock();
  885. return retval;
  886. }
  887. #ifdef __ARCH_WANT_SYS_GETPGRP
  888. SYSCALL_DEFINE0(getpgrp)
  889. {
  890. return sys_getpgid(0);
  891. }
  892. #endif
  893. SYSCALL_DEFINE1(getsid, pid_t, pid)
  894. {
  895. struct task_struct *p;
  896. struct pid *sid;
  897. int retval;
  898. rcu_read_lock();
  899. if (!pid)
  900. sid = task_session(current);
  901. else {
  902. retval = -ESRCH;
  903. p = find_task_by_vpid(pid);
  904. if (!p)
  905. goto out;
  906. sid = task_session(p);
  907. if (!sid)
  908. goto out;
  909. retval = security_task_getsid(p);
  910. if (retval)
  911. goto out;
  912. }
  913. retval = pid_vnr(sid);
  914. out:
  915. rcu_read_unlock();
  916. return retval;
  917. }
  918. static void set_special_pids(struct pid *pid)
  919. {
  920. struct task_struct *curr = current->group_leader;
  921. if (task_session(curr) != pid)
  922. change_pid(curr, PIDTYPE_SID, pid);
  923. if (task_pgrp(curr) != pid)
  924. change_pid(curr, PIDTYPE_PGID, pid);
  925. }
  926. SYSCALL_DEFINE0(setsid)
  927. {
  928. struct task_struct *group_leader = current->group_leader;
  929. struct pid *sid = task_pid(group_leader);
  930. pid_t session = pid_vnr(sid);
  931. int err = -EPERM;
  932. write_lock_irq(&tasklist_lock);
  933. /* Fail if I am already a session leader */
  934. if (group_leader->signal->leader)
  935. goto out;
  936. /* Fail if a process group id already exists that equals the
  937. * proposed session id.
  938. */
  939. if (pid_task(sid, PIDTYPE_PGID))
  940. goto out;
  941. group_leader->signal->leader = 1;
  942. set_special_pids(sid);
  943. proc_clear_tty(group_leader);
  944. err = session;
  945. out:
  946. write_unlock_irq(&tasklist_lock);
  947. if (err > 0) {
  948. proc_sid_connector(group_leader);
  949. sched_autogroup_create_attach(group_leader);
  950. }
  951. return err;
  952. }
  953. DECLARE_RWSEM(uts_sem);
  954. #ifdef COMPAT_UTS_MACHINE
  955. #define override_architecture(name) \
  956. (personality(current->personality) == PER_LINUX32 && \
  957. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  958. sizeof(COMPAT_UTS_MACHINE)))
  959. #else
  960. #define override_architecture(name) 0
  961. #endif
  962. /*
  963. * Work around broken programs that cannot handle "Linux 3.0".
  964. * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
  965. * And we map 4.x to 2.6.60+x, so 4.0 would be 2.6.60.
  966. */
  967. static int override_release(char __user *release, size_t len)
  968. {
  969. int ret = 0;
  970. if (current->personality & UNAME26) {
  971. const char *rest = UTS_RELEASE;
  972. char buf[65] = { 0 };
  973. int ndots = 0;
  974. unsigned v;
  975. size_t copy;
  976. while (*rest) {
  977. if (*rest == '.' && ++ndots >= 3)
  978. break;
  979. if (!isdigit(*rest) && *rest != '.')
  980. break;
  981. rest++;
  982. }
  983. v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60;
  984. copy = clamp_t(size_t, len, 1, sizeof(buf));
  985. copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
  986. ret = copy_to_user(release, buf, copy + 1);
  987. }
  988. return ret;
  989. }
  990. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  991. {
  992. int errno = 0;
  993. down_read(&uts_sem);
  994. if (copy_to_user(name, utsname(), sizeof *name))
  995. errno = -EFAULT;
  996. up_read(&uts_sem);
  997. if (!errno && override_release(name->release, sizeof(name->release)))
  998. errno = -EFAULT;
  999. if (!errno && override_architecture(name))
  1000. errno = -EFAULT;
  1001. return errno;
  1002. }
  1003. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  1004. /*
  1005. * Old cruft
  1006. */
  1007. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  1008. {
  1009. int error = 0;
  1010. if (!name)
  1011. return -EFAULT;
  1012. down_read(&uts_sem);
  1013. if (copy_to_user(name, utsname(), sizeof(*name)))
  1014. error = -EFAULT;
  1015. up_read(&uts_sem);
  1016. if (!error && override_release(name->release, sizeof(name->release)))
  1017. error = -EFAULT;
  1018. if (!error && override_architecture(name))
  1019. error = -EFAULT;
  1020. return error;
  1021. }
  1022. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1023. {
  1024. int error;
  1025. if (!name)
  1026. return -EFAULT;
  1027. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1028. return -EFAULT;
  1029. down_read(&uts_sem);
  1030. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1031. __OLD_UTS_LEN);
  1032. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1033. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1034. __OLD_UTS_LEN);
  1035. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1036. error |= __copy_to_user(&name->release, &utsname()->release,
  1037. __OLD_UTS_LEN);
  1038. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1039. error |= __copy_to_user(&name->version, &utsname()->version,
  1040. __OLD_UTS_LEN);
  1041. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1042. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1043. __OLD_UTS_LEN);
  1044. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1045. up_read(&uts_sem);
  1046. if (!error && override_architecture(name))
  1047. error = -EFAULT;
  1048. if (!error && override_release(name->release, sizeof(name->release)))
  1049. error = -EFAULT;
  1050. return error ? -EFAULT : 0;
  1051. }
  1052. #endif
  1053. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1054. {
  1055. int errno;
  1056. char tmp[__NEW_UTS_LEN];
  1057. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1058. return -EPERM;
  1059. if (len < 0 || len > __NEW_UTS_LEN)
  1060. return -EINVAL;
  1061. down_write(&uts_sem);
  1062. errno = -EFAULT;
  1063. if (!copy_from_user(tmp, name, len)) {
  1064. struct new_utsname *u = utsname();
  1065. memcpy(u->nodename, tmp, len);
  1066. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1067. errno = 0;
  1068. uts_proc_notify(UTS_PROC_HOSTNAME);
  1069. }
  1070. up_write(&uts_sem);
  1071. return errno;
  1072. }
  1073. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1074. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1075. {
  1076. int i, errno;
  1077. struct new_utsname *u;
  1078. if (len < 0)
  1079. return -EINVAL;
  1080. down_read(&uts_sem);
  1081. u = utsname();
  1082. i = 1 + strlen(u->nodename);
  1083. if (i > len)
  1084. i = len;
  1085. errno = 0;
  1086. if (copy_to_user(name, u->nodename, i))
  1087. errno = -EFAULT;
  1088. up_read(&uts_sem);
  1089. return errno;
  1090. }
  1091. #endif
  1092. /*
  1093. * Only setdomainname; getdomainname can be implemented by calling
  1094. * uname()
  1095. */
  1096. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1097. {
  1098. int errno;
  1099. char tmp[__NEW_UTS_LEN];
  1100. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1101. return -EPERM;
  1102. if (len < 0 || len > __NEW_UTS_LEN)
  1103. return -EINVAL;
  1104. down_write(&uts_sem);
  1105. errno = -EFAULT;
  1106. if (!copy_from_user(tmp, name, len)) {
  1107. struct new_utsname *u = utsname();
  1108. memcpy(u->domainname, tmp, len);
  1109. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1110. errno = 0;
  1111. uts_proc_notify(UTS_PROC_DOMAINNAME);
  1112. }
  1113. up_write(&uts_sem);
  1114. return errno;
  1115. }
  1116. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1117. {
  1118. struct rlimit value;
  1119. int ret;
  1120. ret = do_prlimit(current, resource, NULL, &value);
  1121. if (!ret)
  1122. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1123. return ret;
  1124. }
  1125. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1126. /*
  1127. * Back compatibility for getrlimit. Needed for some apps.
  1128. */
  1129. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1130. struct rlimit __user *, rlim)
  1131. {
  1132. struct rlimit x;
  1133. if (resource >= RLIM_NLIMITS)
  1134. return -EINVAL;
  1135. resource = array_index_nospec(resource, RLIM_NLIMITS);
  1136. task_lock(current->group_leader);
  1137. x = current->signal->rlim[resource];
  1138. task_unlock(current->group_leader);
  1139. if (x.rlim_cur > 0x7FFFFFFF)
  1140. x.rlim_cur = 0x7FFFFFFF;
  1141. if (x.rlim_max > 0x7FFFFFFF)
  1142. x.rlim_max = 0x7FFFFFFF;
  1143. return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
  1144. }
  1145. #endif
  1146. static inline bool rlim64_is_infinity(__u64 rlim64)
  1147. {
  1148. #if BITS_PER_LONG < 64
  1149. return rlim64 >= ULONG_MAX;
  1150. #else
  1151. return rlim64 == RLIM64_INFINITY;
  1152. #endif
  1153. }
  1154. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1155. {
  1156. if (rlim->rlim_cur == RLIM_INFINITY)
  1157. rlim64->rlim_cur = RLIM64_INFINITY;
  1158. else
  1159. rlim64->rlim_cur = rlim->rlim_cur;
  1160. if (rlim->rlim_max == RLIM_INFINITY)
  1161. rlim64->rlim_max = RLIM64_INFINITY;
  1162. else
  1163. rlim64->rlim_max = rlim->rlim_max;
  1164. }
  1165. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1166. {
  1167. if (rlim64_is_infinity(rlim64->rlim_cur))
  1168. rlim->rlim_cur = RLIM_INFINITY;
  1169. else
  1170. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1171. if (rlim64_is_infinity(rlim64->rlim_max))
  1172. rlim->rlim_max = RLIM_INFINITY;
  1173. else
  1174. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1175. }
  1176. /* make sure you are allowed to change @tsk limits before calling this */
  1177. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1178. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1179. {
  1180. struct rlimit *rlim;
  1181. int retval = 0;
  1182. if (resource >= RLIM_NLIMITS)
  1183. return -EINVAL;
  1184. if (new_rlim) {
  1185. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1186. return -EINVAL;
  1187. if (resource == RLIMIT_NOFILE &&
  1188. new_rlim->rlim_max > sysctl_nr_open)
  1189. return -EPERM;
  1190. }
  1191. /* protect tsk->signal and tsk->sighand from disappearing */
  1192. read_lock(&tasklist_lock);
  1193. if (!tsk->sighand) {
  1194. retval = -ESRCH;
  1195. goto out;
  1196. }
  1197. rlim = tsk->signal->rlim + resource;
  1198. task_lock(tsk->group_leader);
  1199. if (new_rlim) {
  1200. /* Keep the capable check against init_user_ns until
  1201. cgroups can contain all limits */
  1202. if (new_rlim->rlim_max > rlim->rlim_max &&
  1203. !capable(CAP_SYS_RESOURCE))
  1204. retval = -EPERM;
  1205. if (!retval)
  1206. retval = security_task_setrlimit(tsk->group_leader,
  1207. resource, new_rlim);
  1208. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1209. /*
  1210. * The caller is asking for an immediate RLIMIT_CPU
  1211. * expiry. But we use the zero value to mean "it was
  1212. * never set". So let's cheat and make it one second
  1213. * instead
  1214. */
  1215. new_rlim->rlim_cur = 1;
  1216. }
  1217. }
  1218. if (!retval) {
  1219. if (old_rlim)
  1220. *old_rlim = *rlim;
  1221. if (new_rlim)
  1222. *rlim = *new_rlim;
  1223. }
  1224. task_unlock(tsk->group_leader);
  1225. /*
  1226. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1227. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1228. * very long-standing error, and fixing it now risks breakage of
  1229. * applications, so we live with it
  1230. */
  1231. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1232. new_rlim->rlim_cur != RLIM_INFINITY)
  1233. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1234. out:
  1235. read_unlock(&tasklist_lock);
  1236. return retval;
  1237. }
  1238. /* rcu lock must be held */
  1239. static int check_prlimit_permission(struct task_struct *task)
  1240. {
  1241. const struct cred *cred = current_cred(), *tcred;
  1242. if (current == task)
  1243. return 0;
  1244. tcred = __task_cred(task);
  1245. if (uid_eq(cred->uid, tcred->euid) &&
  1246. uid_eq(cred->uid, tcred->suid) &&
  1247. uid_eq(cred->uid, tcred->uid) &&
  1248. gid_eq(cred->gid, tcred->egid) &&
  1249. gid_eq(cred->gid, tcred->sgid) &&
  1250. gid_eq(cred->gid, tcred->gid))
  1251. return 0;
  1252. if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
  1253. return 0;
  1254. return -EPERM;
  1255. }
  1256. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1257. const struct rlimit64 __user *, new_rlim,
  1258. struct rlimit64 __user *, old_rlim)
  1259. {
  1260. struct rlimit64 old64, new64;
  1261. struct rlimit old, new;
  1262. struct task_struct *tsk;
  1263. int ret;
  1264. if (new_rlim) {
  1265. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1266. return -EFAULT;
  1267. rlim64_to_rlim(&new64, &new);
  1268. }
  1269. rcu_read_lock();
  1270. tsk = pid ? find_task_by_vpid(pid) : current;
  1271. if (!tsk) {
  1272. rcu_read_unlock();
  1273. return -ESRCH;
  1274. }
  1275. ret = check_prlimit_permission(tsk);
  1276. if (ret) {
  1277. rcu_read_unlock();
  1278. return ret;
  1279. }
  1280. get_task_struct(tsk);
  1281. rcu_read_unlock();
  1282. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1283. old_rlim ? &old : NULL);
  1284. if (!ret && old_rlim) {
  1285. rlim_to_rlim64(&old, &old64);
  1286. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1287. ret = -EFAULT;
  1288. }
  1289. put_task_struct(tsk);
  1290. return ret;
  1291. }
  1292. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1293. {
  1294. struct rlimit new_rlim;
  1295. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1296. return -EFAULT;
  1297. return do_prlimit(current, resource, &new_rlim, NULL);
  1298. }
  1299. /*
  1300. * It would make sense to put struct rusage in the task_struct,
  1301. * except that would make the task_struct be *really big*. After
  1302. * task_struct gets moved into malloc'ed memory, it would
  1303. * make sense to do this. It will make moving the rest of the information
  1304. * a lot simpler! (Which we're not doing right now because we're not
  1305. * measuring them yet).
  1306. *
  1307. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1308. * races with threads incrementing their own counters. But since word
  1309. * reads are atomic, we either get new values or old values and we don't
  1310. * care which for the sums. We always take the siglock to protect reading
  1311. * the c* fields from p->signal from races with exit.c updating those
  1312. * fields when reaping, so a sample either gets all the additions of a
  1313. * given child after it's reaped, or none so this sample is before reaping.
  1314. *
  1315. * Locking:
  1316. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1317. * for the cases current multithreaded, non-current single threaded
  1318. * non-current multithreaded. Thread traversal is now safe with
  1319. * the siglock held.
  1320. * Strictly speaking, we donot need to take the siglock if we are current and
  1321. * single threaded, as no one else can take our signal_struct away, no one
  1322. * else can reap the children to update signal->c* counters, and no one else
  1323. * can race with the signal-> fields. If we do not take any lock, the
  1324. * signal-> fields could be read out of order while another thread was just
  1325. * exiting. So we should place a read memory barrier when we avoid the lock.
  1326. * On the writer side, write memory barrier is implied in __exit_signal
  1327. * as __exit_signal releases the siglock spinlock after updating the signal->
  1328. * fields. But we don't do this yet to keep things simple.
  1329. *
  1330. */
  1331. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1332. {
  1333. r->ru_nvcsw += t->nvcsw;
  1334. r->ru_nivcsw += t->nivcsw;
  1335. r->ru_minflt += t->min_flt;
  1336. r->ru_majflt += t->maj_flt;
  1337. r->ru_inblock += task_io_get_inblock(t);
  1338. r->ru_oublock += task_io_get_oublock(t);
  1339. }
  1340. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1341. {
  1342. struct task_struct *t;
  1343. unsigned long flags;
  1344. cputime_t tgutime, tgstime, utime, stime;
  1345. unsigned long maxrss = 0;
  1346. memset((char *)r, 0, sizeof (*r));
  1347. utime = stime = 0;
  1348. if (who == RUSAGE_THREAD) {
  1349. task_cputime_adjusted(current, &utime, &stime);
  1350. accumulate_thread_rusage(p, r);
  1351. maxrss = p->signal->maxrss;
  1352. goto out;
  1353. }
  1354. if (!lock_task_sighand(p, &flags))
  1355. return;
  1356. switch (who) {
  1357. case RUSAGE_BOTH:
  1358. case RUSAGE_CHILDREN:
  1359. utime = p->signal->cutime;
  1360. stime = p->signal->cstime;
  1361. r->ru_nvcsw = p->signal->cnvcsw;
  1362. r->ru_nivcsw = p->signal->cnivcsw;
  1363. r->ru_minflt = p->signal->cmin_flt;
  1364. r->ru_majflt = p->signal->cmaj_flt;
  1365. r->ru_inblock = p->signal->cinblock;
  1366. r->ru_oublock = p->signal->coublock;
  1367. maxrss = p->signal->cmaxrss;
  1368. if (who == RUSAGE_CHILDREN)
  1369. break;
  1370. case RUSAGE_SELF:
  1371. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1372. utime += tgutime;
  1373. stime += tgstime;
  1374. r->ru_nvcsw += p->signal->nvcsw;
  1375. r->ru_nivcsw += p->signal->nivcsw;
  1376. r->ru_minflt += p->signal->min_flt;
  1377. r->ru_majflt += p->signal->maj_flt;
  1378. r->ru_inblock += p->signal->inblock;
  1379. r->ru_oublock += p->signal->oublock;
  1380. if (maxrss < p->signal->maxrss)
  1381. maxrss = p->signal->maxrss;
  1382. t = p;
  1383. do {
  1384. accumulate_thread_rusage(t, r);
  1385. } while_each_thread(p, t);
  1386. break;
  1387. default:
  1388. BUG();
  1389. }
  1390. unlock_task_sighand(p, &flags);
  1391. out:
  1392. cputime_to_timeval(utime, &r->ru_utime);
  1393. cputime_to_timeval(stime, &r->ru_stime);
  1394. if (who != RUSAGE_CHILDREN) {
  1395. struct mm_struct *mm = get_task_mm(p);
  1396. if (mm) {
  1397. setmax_mm_hiwater_rss(&maxrss, mm);
  1398. mmput(mm);
  1399. }
  1400. }
  1401. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1402. }
  1403. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1404. {
  1405. struct rusage r;
  1406. k_getrusage(p, who, &r);
  1407. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1408. }
  1409. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1410. {
  1411. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1412. who != RUSAGE_THREAD)
  1413. return -EINVAL;
  1414. return getrusage(current, who, ru);
  1415. }
  1416. #ifdef CONFIG_COMPAT
  1417. COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
  1418. {
  1419. struct rusage r;
  1420. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1421. who != RUSAGE_THREAD)
  1422. return -EINVAL;
  1423. k_getrusage(current, who, &r);
  1424. return put_compat_rusage(&r, ru);
  1425. }
  1426. #endif
  1427. SYSCALL_DEFINE1(umask, int, mask)
  1428. {
  1429. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1430. return mask;
  1431. }
  1432. static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
  1433. {
  1434. struct fd exe;
  1435. struct file *old_exe, *exe_file;
  1436. struct inode *inode;
  1437. int err;
  1438. exe = fdget(fd);
  1439. if (!exe.file)
  1440. return -EBADF;
  1441. inode = file_inode(exe.file);
  1442. /*
  1443. * Because the original mm->exe_file points to executable file, make
  1444. * sure that this one is executable as well, to avoid breaking an
  1445. * overall picture.
  1446. */
  1447. err = -EACCES;
  1448. if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
  1449. goto exit;
  1450. err = inode_permission(inode, MAY_EXEC);
  1451. if (err)
  1452. goto exit;
  1453. /*
  1454. * Forbid mm->exe_file change if old file still mapped.
  1455. */
  1456. exe_file = get_mm_exe_file(mm);
  1457. err = -EBUSY;
  1458. if (exe_file) {
  1459. struct vm_area_struct *vma;
  1460. down_read(&mm->mmap_sem);
  1461. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1462. if (!vma->vm_file)
  1463. continue;
  1464. if (path_equal(&vma->vm_file->f_path,
  1465. &exe_file->f_path))
  1466. goto exit_err;
  1467. }
  1468. up_read(&mm->mmap_sem);
  1469. fput(exe_file);
  1470. }
  1471. /*
  1472. * The symlink can be changed only once, just to disallow arbitrary
  1473. * transitions malicious software might bring in. This means one
  1474. * could make a snapshot over all processes running and monitor
  1475. * /proc/pid/exe changes to notice unusual activity if needed.
  1476. */
  1477. err = -EPERM;
  1478. if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
  1479. goto exit;
  1480. err = 0;
  1481. /* set the new file, lockless */
  1482. get_file(exe.file);
  1483. old_exe = xchg(&mm->exe_file, exe.file);
  1484. if (old_exe)
  1485. fput(old_exe);
  1486. exit:
  1487. fdput(exe);
  1488. return err;
  1489. exit_err:
  1490. up_read(&mm->mmap_sem);
  1491. fput(exe_file);
  1492. goto exit;
  1493. }
  1494. /*
  1495. * WARNING: we don't require any capability here so be very careful
  1496. * in what is allowed for modification from userspace.
  1497. */
  1498. static int validate_prctl_map(struct prctl_mm_map *prctl_map)
  1499. {
  1500. unsigned long mmap_max_addr = TASK_SIZE;
  1501. struct mm_struct *mm = current->mm;
  1502. int error = -EINVAL, i;
  1503. static const unsigned char offsets[] = {
  1504. offsetof(struct prctl_mm_map, start_code),
  1505. offsetof(struct prctl_mm_map, end_code),
  1506. offsetof(struct prctl_mm_map, start_data),
  1507. offsetof(struct prctl_mm_map, end_data),
  1508. offsetof(struct prctl_mm_map, start_brk),
  1509. offsetof(struct prctl_mm_map, brk),
  1510. offsetof(struct prctl_mm_map, start_stack),
  1511. offsetof(struct prctl_mm_map, arg_start),
  1512. offsetof(struct prctl_mm_map, arg_end),
  1513. offsetof(struct prctl_mm_map, env_start),
  1514. offsetof(struct prctl_mm_map, env_end),
  1515. };
  1516. /*
  1517. * Make sure the members are not somewhere outside
  1518. * of allowed address space.
  1519. */
  1520. for (i = 0; i < ARRAY_SIZE(offsets); i++) {
  1521. u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
  1522. if ((unsigned long)val >= mmap_max_addr ||
  1523. (unsigned long)val < mmap_min_addr)
  1524. goto out;
  1525. }
  1526. /*
  1527. * Make sure the pairs are ordered.
  1528. */
  1529. #define __prctl_check_order(__m1, __op, __m2) \
  1530. ((unsigned long)prctl_map->__m1 __op \
  1531. (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
  1532. error = __prctl_check_order(start_code, <, end_code);
  1533. error |= __prctl_check_order(start_data, <, end_data);
  1534. error |= __prctl_check_order(start_brk, <=, brk);
  1535. error |= __prctl_check_order(arg_start, <=, arg_end);
  1536. error |= __prctl_check_order(env_start, <=, env_end);
  1537. if (error)
  1538. goto out;
  1539. #undef __prctl_check_order
  1540. error = -EINVAL;
  1541. /*
  1542. * @brk should be after @end_data in traditional maps.
  1543. */
  1544. if (prctl_map->start_brk <= prctl_map->end_data ||
  1545. prctl_map->brk <= prctl_map->end_data)
  1546. goto out;
  1547. /*
  1548. * Neither we should allow to override limits if they set.
  1549. */
  1550. if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
  1551. prctl_map->start_brk, prctl_map->end_data,
  1552. prctl_map->start_data))
  1553. goto out;
  1554. /*
  1555. * Someone is trying to cheat the auxv vector.
  1556. */
  1557. if (prctl_map->auxv_size) {
  1558. if (!prctl_map->auxv || prctl_map->auxv_size > sizeof(mm->saved_auxv))
  1559. goto out;
  1560. }
  1561. /*
  1562. * Finally, make sure the caller has the rights to
  1563. * change /proc/pid/exe link: only local root should
  1564. * be allowed to.
  1565. */
  1566. if (prctl_map->exe_fd != (u32)-1) {
  1567. struct user_namespace *ns = current_user_ns();
  1568. const struct cred *cred = current_cred();
  1569. if (!uid_eq(cred->uid, make_kuid(ns, 0)) ||
  1570. !gid_eq(cred->gid, make_kgid(ns, 0)))
  1571. goto out;
  1572. }
  1573. error = 0;
  1574. out:
  1575. return error;
  1576. }
  1577. #ifdef CONFIG_CHECKPOINT_RESTORE
  1578. static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
  1579. {
  1580. struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
  1581. unsigned long user_auxv[AT_VECTOR_SIZE];
  1582. struct mm_struct *mm = current->mm;
  1583. int error;
  1584. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1585. BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
  1586. if (opt == PR_SET_MM_MAP_SIZE)
  1587. return put_user((unsigned int)sizeof(prctl_map),
  1588. (unsigned int __user *)addr);
  1589. if (data_size != sizeof(prctl_map))
  1590. return -EINVAL;
  1591. if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
  1592. return -EFAULT;
  1593. error = validate_prctl_map(&prctl_map);
  1594. if (error)
  1595. return error;
  1596. if (prctl_map.auxv_size) {
  1597. memset(user_auxv, 0, sizeof(user_auxv));
  1598. if (copy_from_user(user_auxv,
  1599. (const void __user *)prctl_map.auxv,
  1600. prctl_map.auxv_size))
  1601. return -EFAULT;
  1602. /* Last entry must be AT_NULL as specification requires */
  1603. user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
  1604. user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
  1605. }
  1606. if (prctl_map.exe_fd != (u32)-1) {
  1607. error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
  1608. if (error)
  1609. return error;
  1610. }
  1611. down_write(&mm->mmap_sem);
  1612. /*
  1613. * We don't validate if these members are pointing to
  1614. * real present VMAs because application may have correspond
  1615. * VMAs already unmapped and kernel uses these members for statistics
  1616. * output in procfs mostly, except
  1617. *
  1618. * - @start_brk/@brk which are used in do_brk but kernel lookups
  1619. * for VMAs when updating these memvers so anything wrong written
  1620. * here cause kernel to swear at userspace program but won't lead
  1621. * to any problem in kernel itself
  1622. */
  1623. mm->start_code = prctl_map.start_code;
  1624. mm->end_code = prctl_map.end_code;
  1625. mm->start_data = prctl_map.start_data;
  1626. mm->end_data = prctl_map.end_data;
  1627. mm->start_brk = prctl_map.start_brk;
  1628. mm->brk = prctl_map.brk;
  1629. mm->start_stack = prctl_map.start_stack;
  1630. mm->arg_start = prctl_map.arg_start;
  1631. mm->arg_end = prctl_map.arg_end;
  1632. mm->env_start = prctl_map.env_start;
  1633. mm->env_end = prctl_map.env_end;
  1634. /*
  1635. * Note this update of @saved_auxv is lockless thus
  1636. * if someone reads this member in procfs while we're
  1637. * updating -- it may get partly updated results. It's
  1638. * known and acceptable trade off: we leave it as is to
  1639. * not introduce additional locks here making the kernel
  1640. * more complex.
  1641. */
  1642. if (prctl_map.auxv_size)
  1643. memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
  1644. up_write(&mm->mmap_sem);
  1645. return 0;
  1646. }
  1647. #endif /* CONFIG_CHECKPOINT_RESTORE */
  1648. static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
  1649. unsigned long len)
  1650. {
  1651. /*
  1652. * This doesn't move the auxiliary vector itself since it's pinned to
  1653. * mm_struct, but it permits filling the vector with new values. It's
  1654. * up to the caller to provide sane values here, otherwise userspace
  1655. * tools which use this vector might be unhappy.
  1656. */
  1657. unsigned long user_auxv[AT_VECTOR_SIZE];
  1658. if (len > sizeof(user_auxv))
  1659. return -EINVAL;
  1660. if (copy_from_user(user_auxv, (const void __user *)addr, len))
  1661. return -EFAULT;
  1662. /* Make sure the last entry is always AT_NULL */
  1663. user_auxv[AT_VECTOR_SIZE - 2] = 0;
  1664. user_auxv[AT_VECTOR_SIZE - 1] = 0;
  1665. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1666. task_lock(current);
  1667. memcpy(mm->saved_auxv, user_auxv, len);
  1668. task_unlock(current);
  1669. return 0;
  1670. }
  1671. static int prctl_set_mm(int opt, unsigned long addr,
  1672. unsigned long arg4, unsigned long arg5)
  1673. {
  1674. struct mm_struct *mm = current->mm;
  1675. struct prctl_mm_map prctl_map;
  1676. struct vm_area_struct *vma;
  1677. int error;
  1678. if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
  1679. opt != PR_SET_MM_MAP &&
  1680. opt != PR_SET_MM_MAP_SIZE)))
  1681. return -EINVAL;
  1682. #ifdef CONFIG_CHECKPOINT_RESTORE
  1683. if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
  1684. return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
  1685. #endif
  1686. if (!capable(CAP_SYS_RESOURCE))
  1687. return -EPERM;
  1688. if (opt == PR_SET_MM_EXE_FILE)
  1689. return prctl_set_mm_exe_file(mm, (unsigned int)addr);
  1690. if (opt == PR_SET_MM_AUXV)
  1691. return prctl_set_auxv(mm, addr, arg4);
  1692. if (addr >= TASK_SIZE || addr < mmap_min_addr)
  1693. return -EINVAL;
  1694. error = -EINVAL;
  1695. down_write(&mm->mmap_sem);
  1696. vma = find_vma(mm, addr);
  1697. prctl_map.start_code = mm->start_code;
  1698. prctl_map.end_code = mm->end_code;
  1699. prctl_map.start_data = mm->start_data;
  1700. prctl_map.end_data = mm->end_data;
  1701. prctl_map.start_brk = mm->start_brk;
  1702. prctl_map.brk = mm->brk;
  1703. prctl_map.start_stack = mm->start_stack;
  1704. prctl_map.arg_start = mm->arg_start;
  1705. prctl_map.arg_end = mm->arg_end;
  1706. prctl_map.env_start = mm->env_start;
  1707. prctl_map.env_end = mm->env_end;
  1708. prctl_map.auxv = NULL;
  1709. prctl_map.auxv_size = 0;
  1710. prctl_map.exe_fd = -1;
  1711. switch (opt) {
  1712. case PR_SET_MM_START_CODE:
  1713. prctl_map.start_code = addr;
  1714. break;
  1715. case PR_SET_MM_END_CODE:
  1716. prctl_map.end_code = addr;
  1717. break;
  1718. case PR_SET_MM_START_DATA:
  1719. prctl_map.start_data = addr;
  1720. break;
  1721. case PR_SET_MM_END_DATA:
  1722. prctl_map.end_data = addr;
  1723. break;
  1724. case PR_SET_MM_START_STACK:
  1725. prctl_map.start_stack = addr;
  1726. break;
  1727. case PR_SET_MM_START_BRK:
  1728. prctl_map.start_brk = addr;
  1729. break;
  1730. case PR_SET_MM_BRK:
  1731. prctl_map.brk = addr;
  1732. break;
  1733. case PR_SET_MM_ARG_START:
  1734. prctl_map.arg_start = addr;
  1735. break;
  1736. case PR_SET_MM_ARG_END:
  1737. prctl_map.arg_end = addr;
  1738. break;
  1739. case PR_SET_MM_ENV_START:
  1740. prctl_map.env_start = addr;
  1741. break;
  1742. case PR_SET_MM_ENV_END:
  1743. prctl_map.env_end = addr;
  1744. break;
  1745. default:
  1746. goto out;
  1747. }
  1748. error = validate_prctl_map(&prctl_map);
  1749. if (error)
  1750. goto out;
  1751. switch (opt) {
  1752. /*
  1753. * If command line arguments and environment
  1754. * are placed somewhere else on stack, we can
  1755. * set them up here, ARG_START/END to setup
  1756. * command line argumets and ENV_START/END
  1757. * for environment.
  1758. */
  1759. case PR_SET_MM_START_STACK:
  1760. case PR_SET_MM_ARG_START:
  1761. case PR_SET_MM_ARG_END:
  1762. case PR_SET_MM_ENV_START:
  1763. case PR_SET_MM_ENV_END:
  1764. if (!vma) {
  1765. error = -EFAULT;
  1766. goto out;
  1767. }
  1768. }
  1769. mm->start_code = prctl_map.start_code;
  1770. mm->end_code = prctl_map.end_code;
  1771. mm->start_data = prctl_map.start_data;
  1772. mm->end_data = prctl_map.end_data;
  1773. mm->start_brk = prctl_map.start_brk;
  1774. mm->brk = prctl_map.brk;
  1775. mm->start_stack = prctl_map.start_stack;
  1776. mm->arg_start = prctl_map.arg_start;
  1777. mm->arg_end = prctl_map.arg_end;
  1778. mm->env_start = prctl_map.env_start;
  1779. mm->env_end = prctl_map.env_end;
  1780. error = 0;
  1781. out:
  1782. up_write(&mm->mmap_sem);
  1783. return error;
  1784. }
  1785. #ifdef CONFIG_CHECKPOINT_RESTORE
  1786. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1787. {
  1788. return put_user(me->clear_child_tid, tid_addr);
  1789. }
  1790. #else
  1791. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1792. {
  1793. return -EINVAL;
  1794. }
  1795. #endif
  1796. int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
  1797. {
  1798. return -EINVAL;
  1799. }
  1800. int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
  1801. unsigned long ctrl)
  1802. {
  1803. return -EINVAL;
  1804. }
  1805. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1806. unsigned long, arg4, unsigned long, arg5)
  1807. {
  1808. struct task_struct *me = current;
  1809. unsigned char comm[sizeof(me->comm)];
  1810. long error;
  1811. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1812. if (error != -ENOSYS)
  1813. return error;
  1814. error = 0;
  1815. switch (option) {
  1816. case PR_SET_PDEATHSIG:
  1817. if (!valid_signal(arg2)) {
  1818. error = -EINVAL;
  1819. break;
  1820. }
  1821. me->pdeath_signal = arg2;
  1822. break;
  1823. case PR_GET_PDEATHSIG:
  1824. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1825. break;
  1826. case PR_GET_DUMPABLE:
  1827. error = get_dumpable(me->mm);
  1828. break;
  1829. case PR_SET_DUMPABLE:
  1830. if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
  1831. error = -EINVAL;
  1832. break;
  1833. }
  1834. set_dumpable(me->mm, arg2);
  1835. break;
  1836. case PR_SET_UNALIGN:
  1837. error = SET_UNALIGN_CTL(me, arg2);
  1838. break;
  1839. case PR_GET_UNALIGN:
  1840. error = GET_UNALIGN_CTL(me, arg2);
  1841. break;
  1842. case PR_SET_FPEMU:
  1843. error = SET_FPEMU_CTL(me, arg2);
  1844. break;
  1845. case PR_GET_FPEMU:
  1846. error = GET_FPEMU_CTL(me, arg2);
  1847. break;
  1848. case PR_SET_FPEXC:
  1849. error = SET_FPEXC_CTL(me, arg2);
  1850. break;
  1851. case PR_GET_FPEXC:
  1852. error = GET_FPEXC_CTL(me, arg2);
  1853. break;
  1854. case PR_GET_TIMING:
  1855. error = PR_TIMING_STATISTICAL;
  1856. break;
  1857. case PR_SET_TIMING:
  1858. if (arg2 != PR_TIMING_STATISTICAL)
  1859. error = -EINVAL;
  1860. break;
  1861. case PR_SET_NAME:
  1862. comm[sizeof(me->comm) - 1] = 0;
  1863. if (strncpy_from_user(comm, (char __user *)arg2,
  1864. sizeof(me->comm) - 1) < 0)
  1865. return -EFAULT;
  1866. set_task_comm(me, comm);
  1867. proc_comm_connector(me);
  1868. break;
  1869. case PR_GET_NAME:
  1870. get_task_comm(comm, me);
  1871. if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
  1872. return -EFAULT;
  1873. break;
  1874. case PR_GET_ENDIAN:
  1875. error = GET_ENDIAN(me, arg2);
  1876. break;
  1877. case PR_SET_ENDIAN:
  1878. error = SET_ENDIAN(me, arg2);
  1879. break;
  1880. case PR_GET_SECCOMP:
  1881. error = prctl_get_seccomp();
  1882. break;
  1883. case PR_SET_SECCOMP:
  1884. error = prctl_set_seccomp(arg2, (char __user *)arg3);
  1885. break;
  1886. case PR_GET_TSC:
  1887. error = GET_TSC_CTL(arg2);
  1888. break;
  1889. case PR_SET_TSC:
  1890. error = SET_TSC_CTL(arg2);
  1891. break;
  1892. case PR_TASK_PERF_EVENTS_DISABLE:
  1893. error = perf_event_task_disable();
  1894. break;
  1895. case PR_TASK_PERF_EVENTS_ENABLE:
  1896. error = perf_event_task_enable();
  1897. break;
  1898. case PR_GET_TIMERSLACK:
  1899. if (current->timer_slack_ns > ULONG_MAX)
  1900. error = ULONG_MAX;
  1901. else
  1902. error = current->timer_slack_ns;
  1903. break;
  1904. case PR_SET_TIMERSLACK:
  1905. if (arg2 <= 0)
  1906. current->timer_slack_ns =
  1907. current->default_timer_slack_ns;
  1908. else
  1909. current->timer_slack_ns = arg2;
  1910. break;
  1911. case PR_MCE_KILL:
  1912. if (arg4 | arg5)
  1913. return -EINVAL;
  1914. switch (arg2) {
  1915. case PR_MCE_KILL_CLEAR:
  1916. if (arg3 != 0)
  1917. return -EINVAL;
  1918. current->flags &= ~PF_MCE_PROCESS;
  1919. break;
  1920. case PR_MCE_KILL_SET:
  1921. current->flags |= PF_MCE_PROCESS;
  1922. if (arg3 == PR_MCE_KILL_EARLY)
  1923. current->flags |= PF_MCE_EARLY;
  1924. else if (arg3 == PR_MCE_KILL_LATE)
  1925. current->flags &= ~PF_MCE_EARLY;
  1926. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1927. current->flags &=
  1928. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1929. else
  1930. return -EINVAL;
  1931. break;
  1932. default:
  1933. return -EINVAL;
  1934. }
  1935. break;
  1936. case PR_MCE_KILL_GET:
  1937. if (arg2 | arg3 | arg4 | arg5)
  1938. return -EINVAL;
  1939. if (current->flags & PF_MCE_PROCESS)
  1940. error = (current->flags & PF_MCE_EARLY) ?
  1941. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1942. else
  1943. error = PR_MCE_KILL_DEFAULT;
  1944. break;
  1945. case PR_SET_MM:
  1946. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  1947. break;
  1948. case PR_GET_TID_ADDRESS:
  1949. error = prctl_get_tid_address(me, (int __user **)arg2);
  1950. break;
  1951. case PR_SET_CHILD_SUBREAPER:
  1952. me->signal->is_child_subreaper = !!arg2;
  1953. break;
  1954. case PR_GET_CHILD_SUBREAPER:
  1955. error = put_user(me->signal->is_child_subreaper,
  1956. (int __user *)arg2);
  1957. break;
  1958. case PR_SET_NO_NEW_PRIVS:
  1959. if (arg2 != 1 || arg3 || arg4 || arg5)
  1960. return -EINVAL;
  1961. task_set_no_new_privs(current);
  1962. break;
  1963. case PR_GET_NO_NEW_PRIVS:
  1964. if (arg2 || arg3 || arg4 || arg5)
  1965. return -EINVAL;
  1966. return task_no_new_privs(current) ? 1 : 0;
  1967. case PR_GET_THP_DISABLE:
  1968. if (arg2 || arg3 || arg4 || arg5)
  1969. return -EINVAL;
  1970. error = !!(me->mm->def_flags & VM_NOHUGEPAGE);
  1971. break;
  1972. case PR_SET_THP_DISABLE:
  1973. if (arg3 || arg4 || arg5)
  1974. return -EINVAL;
  1975. if (down_write_killable(&me->mm->mmap_sem))
  1976. return -EINTR;
  1977. if (arg2)
  1978. me->mm->def_flags |= VM_NOHUGEPAGE;
  1979. else
  1980. me->mm->def_flags &= ~VM_NOHUGEPAGE;
  1981. up_write(&me->mm->mmap_sem);
  1982. break;
  1983. case PR_MPX_ENABLE_MANAGEMENT:
  1984. if (arg2 || arg3 || arg4 || arg5)
  1985. return -EINVAL;
  1986. error = MPX_ENABLE_MANAGEMENT();
  1987. break;
  1988. case PR_MPX_DISABLE_MANAGEMENT:
  1989. if (arg2 || arg3 || arg4 || arg5)
  1990. return -EINVAL;
  1991. error = MPX_DISABLE_MANAGEMENT();
  1992. break;
  1993. case PR_SET_FP_MODE:
  1994. error = SET_FP_MODE(me, arg2);
  1995. break;
  1996. case PR_GET_FP_MODE:
  1997. error = GET_FP_MODE(me);
  1998. break;
  1999. case PR_GET_SPECULATION_CTRL:
  2000. if (arg3 || arg4 || arg5)
  2001. return -EINVAL;
  2002. error = arch_prctl_spec_ctrl_get(me, arg2);
  2003. break;
  2004. case PR_SET_SPECULATION_CTRL:
  2005. if (arg4 || arg5)
  2006. return -EINVAL;
  2007. error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
  2008. break;
  2009. default:
  2010. error = -EINVAL;
  2011. break;
  2012. }
  2013. return error;
  2014. }
  2015. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  2016. struct getcpu_cache __user *, unused)
  2017. {
  2018. int err = 0;
  2019. int cpu = raw_smp_processor_id();
  2020. if (cpup)
  2021. err |= put_user(cpu, cpup);
  2022. if (nodep)
  2023. err |= put_user(cpu_to_node(cpu), nodep);
  2024. return err ? -EFAULT : 0;
  2025. }
  2026. /**
  2027. * do_sysinfo - fill in sysinfo struct
  2028. * @info: pointer to buffer to fill
  2029. */
  2030. static int do_sysinfo(struct sysinfo *info)
  2031. {
  2032. unsigned long mem_total, sav_total;
  2033. unsigned int mem_unit, bitcount;
  2034. struct timespec tp;
  2035. memset(info, 0, sizeof(struct sysinfo));
  2036. get_monotonic_boottime(&tp);
  2037. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  2038. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  2039. info->procs = nr_threads;
  2040. si_meminfo(info);
  2041. si_swapinfo(info);
  2042. /*
  2043. * If the sum of all the available memory (i.e. ram + swap)
  2044. * is less than can be stored in a 32 bit unsigned long then
  2045. * we can be binary compatible with 2.2.x kernels. If not,
  2046. * well, in that case 2.2.x was broken anyways...
  2047. *
  2048. * -Erik Andersen <andersee@debian.org>
  2049. */
  2050. mem_total = info->totalram + info->totalswap;
  2051. if (mem_total < info->totalram || mem_total < info->totalswap)
  2052. goto out;
  2053. bitcount = 0;
  2054. mem_unit = info->mem_unit;
  2055. while (mem_unit > 1) {
  2056. bitcount++;
  2057. mem_unit >>= 1;
  2058. sav_total = mem_total;
  2059. mem_total <<= 1;
  2060. if (mem_total < sav_total)
  2061. goto out;
  2062. }
  2063. /*
  2064. * If mem_total did not overflow, multiply all memory values by
  2065. * info->mem_unit and set it to 1. This leaves things compatible
  2066. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  2067. * kernels...
  2068. */
  2069. info->mem_unit = 1;
  2070. info->totalram <<= bitcount;
  2071. info->freeram <<= bitcount;
  2072. info->sharedram <<= bitcount;
  2073. info->bufferram <<= bitcount;
  2074. info->totalswap <<= bitcount;
  2075. info->freeswap <<= bitcount;
  2076. info->totalhigh <<= bitcount;
  2077. info->freehigh <<= bitcount;
  2078. out:
  2079. return 0;
  2080. }
  2081. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  2082. {
  2083. struct sysinfo val;
  2084. do_sysinfo(&val);
  2085. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  2086. return -EFAULT;
  2087. return 0;
  2088. }
  2089. #ifdef CONFIG_COMPAT
  2090. struct compat_sysinfo {
  2091. s32 uptime;
  2092. u32 loads[3];
  2093. u32 totalram;
  2094. u32 freeram;
  2095. u32 sharedram;
  2096. u32 bufferram;
  2097. u32 totalswap;
  2098. u32 freeswap;
  2099. u16 procs;
  2100. u16 pad;
  2101. u32 totalhigh;
  2102. u32 freehigh;
  2103. u32 mem_unit;
  2104. char _f[20-2*sizeof(u32)-sizeof(int)];
  2105. };
  2106. COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
  2107. {
  2108. struct sysinfo s;
  2109. do_sysinfo(&s);
  2110. /* Check to see if any memory value is too large for 32-bit and scale
  2111. * down if needed
  2112. */
  2113. if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
  2114. int bitcount = 0;
  2115. while (s.mem_unit < PAGE_SIZE) {
  2116. s.mem_unit <<= 1;
  2117. bitcount++;
  2118. }
  2119. s.totalram >>= bitcount;
  2120. s.freeram >>= bitcount;
  2121. s.sharedram >>= bitcount;
  2122. s.bufferram >>= bitcount;
  2123. s.totalswap >>= bitcount;
  2124. s.freeswap >>= bitcount;
  2125. s.totalhigh >>= bitcount;
  2126. s.freehigh >>= bitcount;
  2127. }
  2128. if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
  2129. __put_user(s.uptime, &info->uptime) ||
  2130. __put_user(s.loads[0], &info->loads[0]) ||
  2131. __put_user(s.loads[1], &info->loads[1]) ||
  2132. __put_user(s.loads[2], &info->loads[2]) ||
  2133. __put_user(s.totalram, &info->totalram) ||
  2134. __put_user(s.freeram, &info->freeram) ||
  2135. __put_user(s.sharedram, &info->sharedram) ||
  2136. __put_user(s.bufferram, &info->bufferram) ||
  2137. __put_user(s.totalswap, &info->totalswap) ||
  2138. __put_user(s.freeswap, &info->freeswap) ||
  2139. __put_user(s.procs, &info->procs) ||
  2140. __put_user(s.totalhigh, &info->totalhigh) ||
  2141. __put_user(s.freehigh, &info->freehigh) ||
  2142. __put_user(s.mem_unit, &info->mem_unit))
  2143. return -EFAULT;
  2144. return 0;
  2145. }
  2146. #endif /* CONFIG_COMPAT */