sys.c 50 KB

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