sys.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.h>
  16. #include <linux/perf_event.h>
  17. #include <linux/resource.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kexec.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/capability.h>
  22. #include <linux/device.h>
  23. #include <linux/key.h>
  24. #include <linux/times.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/security.h>
  27. #include <linux/dcookies.h>
  28. #include <linux/suspend.h>
  29. #include <linux/tty.h>
  30. #include <linux/signal.h>
  31. #include <linux/cn_proc.h>
  32. #include <linux/getcpu.h>
  33. #include <linux/task_io_accounting_ops.h>
  34. #include <linux/seccomp.h>
  35. #include <linux/cpu.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/fs_struct.h>
  38. #include <linux/compat.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/kprobes.h>
  41. #include <linux/user_namespace.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/io.h>
  44. #include <asm/unistd.h>
  45. #ifndef SET_UNALIGN_CTL
  46. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  47. #endif
  48. #ifndef GET_UNALIGN_CTL
  49. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  50. #endif
  51. #ifndef SET_FPEMU_CTL
  52. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  53. #endif
  54. #ifndef GET_FPEMU_CTL
  55. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  56. #endif
  57. #ifndef SET_FPEXC_CTL
  58. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  59. #endif
  60. #ifndef GET_FPEXC_CTL
  61. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  62. #endif
  63. #ifndef GET_ENDIAN
  64. # define GET_ENDIAN(a,b) (-EINVAL)
  65. #endif
  66. #ifndef SET_ENDIAN
  67. # define SET_ENDIAN(a,b) (-EINVAL)
  68. #endif
  69. #ifndef GET_TSC_CTL
  70. # define GET_TSC_CTL(a) (-EINVAL)
  71. #endif
  72. #ifndef SET_TSC_CTL
  73. # define SET_TSC_CTL(a) (-EINVAL)
  74. #endif
  75. /*
  76. * this is where the system-wide overflow UID and GID are defined, for
  77. * architectures that now have 32-bit UID/GID but didn't in the past
  78. */
  79. int overflowuid = DEFAULT_OVERFLOWUID;
  80. int overflowgid = DEFAULT_OVERFLOWGID;
  81. #ifdef CONFIG_UID16
  82. EXPORT_SYMBOL(overflowuid);
  83. EXPORT_SYMBOL(overflowgid);
  84. #endif
  85. /*
  86. * the same as above, but for filesystems which can only store a 16-bit
  87. * UID and GID. as such, this is needed on all architectures
  88. */
  89. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  90. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  91. EXPORT_SYMBOL(fs_overflowuid);
  92. EXPORT_SYMBOL(fs_overflowgid);
  93. /*
  94. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  95. */
  96. int C_A_D = 1;
  97. struct pid *cad_pid;
  98. EXPORT_SYMBOL(cad_pid);
  99. /*
  100. * If set, this is used for preparing the system to power off.
  101. */
  102. void (*pm_power_off_prepare)(void);
  103. /*
  104. * set the priority of a task
  105. * - the caller must hold the RCU read lock
  106. */
  107. static int set_one_prio(struct task_struct *p, int niceval, int error)
  108. {
  109. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  110. int no_nice;
  111. if (pcred->uid != cred->euid &&
  112. pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
  113. error = -EPERM;
  114. goto out;
  115. }
  116. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  117. error = -EACCES;
  118. goto out;
  119. }
  120. no_nice = security_task_setnice(p, niceval);
  121. if (no_nice) {
  122. error = no_nice;
  123. goto out;
  124. }
  125. if (error == -ESRCH)
  126. error = 0;
  127. set_user_nice(p, niceval);
  128. out:
  129. return error;
  130. }
  131. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  132. {
  133. struct task_struct *g, *p;
  134. struct user_struct *user;
  135. const struct cred *cred = current_cred();
  136. int error = -EINVAL;
  137. struct pid *pgrp;
  138. if (which > PRIO_USER || which < PRIO_PROCESS)
  139. goto out;
  140. /* normalize: avoid signed division (rounding problems) */
  141. error = -ESRCH;
  142. if (niceval < -20)
  143. niceval = -20;
  144. if (niceval > 19)
  145. niceval = 19;
  146. read_lock(&tasklist_lock);
  147. switch (which) {
  148. case PRIO_PROCESS:
  149. if (who)
  150. p = find_task_by_vpid(who);
  151. else
  152. p = current;
  153. if (p)
  154. error = set_one_prio(p, niceval, error);
  155. break;
  156. case PRIO_PGRP:
  157. if (who)
  158. pgrp = find_vpid(who);
  159. else
  160. pgrp = task_pgrp(current);
  161. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  162. error = set_one_prio(p, niceval, error);
  163. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  164. break;
  165. case PRIO_USER:
  166. user = (struct user_struct *) cred->user;
  167. if (!who)
  168. who = cred->uid;
  169. else if ((who != cred->uid) &&
  170. !(user = find_user(who)))
  171. goto out_unlock; /* No processes for this user */
  172. do_each_thread(g, p)
  173. if (__task_cred(p)->uid == who)
  174. error = set_one_prio(p, niceval, error);
  175. while_each_thread(g, p);
  176. if (who != cred->uid)
  177. free_uid(user); /* For find_user() */
  178. break;
  179. }
  180. out_unlock:
  181. read_unlock(&tasklist_lock);
  182. out:
  183. return error;
  184. }
  185. /*
  186. * Ugh. To avoid negative return values, "getpriority()" will
  187. * not return the normal nice-value, but a negated value that
  188. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  189. * to stay compatible.
  190. */
  191. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  192. {
  193. struct task_struct *g, *p;
  194. struct user_struct *user;
  195. const struct cred *cred = current_cred();
  196. long niceval, retval = -ESRCH;
  197. struct pid *pgrp;
  198. if (which > PRIO_USER || which < PRIO_PROCESS)
  199. return -EINVAL;
  200. read_lock(&tasklist_lock);
  201. switch (which) {
  202. case PRIO_PROCESS:
  203. if (who)
  204. p = find_task_by_vpid(who);
  205. else
  206. p = current;
  207. if (p) {
  208. niceval = 20 - task_nice(p);
  209. if (niceval > retval)
  210. retval = niceval;
  211. }
  212. break;
  213. case PRIO_PGRP:
  214. if (who)
  215. pgrp = find_vpid(who);
  216. else
  217. pgrp = task_pgrp(current);
  218. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  219. niceval = 20 - task_nice(p);
  220. if (niceval > retval)
  221. retval = niceval;
  222. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  223. break;
  224. case PRIO_USER:
  225. user = (struct user_struct *) cred->user;
  226. if (!who)
  227. who = cred->uid;
  228. else if ((who != cred->uid) &&
  229. !(user = find_user(who)))
  230. goto out_unlock; /* No processes for this user */
  231. do_each_thread(g, p)
  232. if (__task_cred(p)->uid == who) {
  233. niceval = 20 - task_nice(p);
  234. if (niceval > retval)
  235. retval = niceval;
  236. }
  237. while_each_thread(g, p);
  238. if (who != cred->uid)
  239. free_uid(user); /* for find_user() */
  240. break;
  241. }
  242. out_unlock:
  243. read_unlock(&tasklist_lock);
  244. return retval;
  245. }
  246. /**
  247. * emergency_restart - reboot the system
  248. *
  249. * Without shutting down any hardware or taking any locks
  250. * reboot the system. This is called when we know we are in
  251. * trouble so this is our best effort to reboot. This is
  252. * safe to call in interrupt context.
  253. */
  254. void emergency_restart(void)
  255. {
  256. machine_emergency_restart();
  257. }
  258. EXPORT_SYMBOL_GPL(emergency_restart);
  259. void kernel_restart_prepare(char *cmd)
  260. {
  261. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  262. system_state = SYSTEM_RESTART;
  263. device_shutdown();
  264. sysdev_shutdown();
  265. }
  266. /**
  267. * kernel_restart - reboot the system
  268. * @cmd: pointer to buffer containing command to execute for restart
  269. * or %NULL
  270. *
  271. * Shutdown everything and perform a clean reboot.
  272. * This is not safe to call in interrupt context.
  273. */
  274. void kernel_restart(char *cmd)
  275. {
  276. kernel_restart_prepare(cmd);
  277. if (!cmd)
  278. printk(KERN_EMERG "Restarting system.\n");
  279. else
  280. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  281. machine_restart(cmd);
  282. }
  283. EXPORT_SYMBOL_GPL(kernel_restart);
  284. static void kernel_shutdown_prepare(enum system_states state)
  285. {
  286. blocking_notifier_call_chain(&reboot_notifier_list,
  287. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  288. system_state = state;
  289. device_shutdown();
  290. }
  291. /**
  292. * kernel_halt - halt the system
  293. *
  294. * Shutdown everything and perform a clean system halt.
  295. */
  296. void kernel_halt(void)
  297. {
  298. kernel_shutdown_prepare(SYSTEM_HALT);
  299. sysdev_shutdown();
  300. printk(KERN_EMERG "System halted.\n");
  301. machine_halt();
  302. }
  303. EXPORT_SYMBOL_GPL(kernel_halt);
  304. /**
  305. * kernel_power_off - power_off the system
  306. *
  307. * Shutdown everything and perform a clean system power_off.
  308. */
  309. void kernel_power_off(void)
  310. {
  311. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  312. if (pm_power_off_prepare)
  313. pm_power_off_prepare();
  314. disable_nonboot_cpus();
  315. sysdev_shutdown();
  316. printk(KERN_EMERG "Power down.\n");
  317. machine_power_off();
  318. }
  319. EXPORT_SYMBOL_GPL(kernel_power_off);
  320. /*
  321. * Reboot system call: for obvious reasons only root may call it,
  322. * and even root needs to set up some magic numbers in the registers
  323. * so that some mistake won't make this reboot the whole machine.
  324. * You can also set the meaning of the ctrl-alt-del-key here.
  325. *
  326. * reboot doesn't sync: do that yourself before calling this.
  327. */
  328. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  329. void __user *, arg)
  330. {
  331. char buffer[256];
  332. int ret = 0;
  333. /* We only trust the superuser with rebooting the system. */
  334. if (!capable(CAP_SYS_BOOT))
  335. return -EPERM;
  336. /* For safety, we require "magic" arguments. */
  337. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  338. (magic2 != LINUX_REBOOT_MAGIC2 &&
  339. magic2 != LINUX_REBOOT_MAGIC2A &&
  340. magic2 != LINUX_REBOOT_MAGIC2B &&
  341. magic2 != LINUX_REBOOT_MAGIC2C))
  342. return -EINVAL;
  343. /* Instead of trying to make the power_off code look like
  344. * halt when pm_power_off is not set do it the easy way.
  345. */
  346. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  347. cmd = LINUX_REBOOT_CMD_HALT;
  348. lock_kernel();
  349. switch (cmd) {
  350. case LINUX_REBOOT_CMD_RESTART:
  351. kernel_restart(NULL);
  352. break;
  353. case LINUX_REBOOT_CMD_CAD_ON:
  354. C_A_D = 1;
  355. break;
  356. case LINUX_REBOOT_CMD_CAD_OFF:
  357. C_A_D = 0;
  358. break;
  359. case LINUX_REBOOT_CMD_HALT:
  360. kernel_halt();
  361. unlock_kernel();
  362. do_exit(0);
  363. panic("cannot halt");
  364. case LINUX_REBOOT_CMD_POWER_OFF:
  365. kernel_power_off();
  366. unlock_kernel();
  367. do_exit(0);
  368. break;
  369. case LINUX_REBOOT_CMD_RESTART2:
  370. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  371. unlock_kernel();
  372. return -EFAULT;
  373. }
  374. buffer[sizeof(buffer) - 1] = '\0';
  375. kernel_restart(buffer);
  376. break;
  377. #ifdef CONFIG_KEXEC
  378. case LINUX_REBOOT_CMD_KEXEC:
  379. ret = kernel_kexec();
  380. break;
  381. #endif
  382. #ifdef CONFIG_HIBERNATION
  383. case LINUX_REBOOT_CMD_SW_SUSPEND:
  384. ret = hibernate();
  385. break;
  386. #endif
  387. default:
  388. ret = -EINVAL;
  389. break;
  390. }
  391. unlock_kernel();
  392. return ret;
  393. }
  394. static void deferred_cad(struct work_struct *dummy)
  395. {
  396. kernel_restart(NULL);
  397. }
  398. /*
  399. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  400. * As it's called within an interrupt, it may NOT sync: the only choice
  401. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  402. */
  403. void ctrl_alt_del(void)
  404. {
  405. static DECLARE_WORK(cad_work, deferred_cad);
  406. if (C_A_D)
  407. schedule_work(&cad_work);
  408. else
  409. kill_cad_pid(SIGINT, 1);
  410. }
  411. /*
  412. * Unprivileged users may change the real gid to the effective gid
  413. * or vice versa. (BSD-style)
  414. *
  415. * If you set the real gid at all, or set the effective gid to a value not
  416. * equal to the real gid, then the saved gid is set to the new effective gid.
  417. *
  418. * This makes it possible for a setgid program to completely drop its
  419. * privileges, which is often a useful assertion to make when you are doing
  420. * a security audit over a program.
  421. *
  422. * The general idea is that a program which uses just setregid() will be
  423. * 100% compatible with BSD. A program which uses just setgid() will be
  424. * 100% compatible with POSIX with saved IDs.
  425. *
  426. * SMP: There are not races, the GIDs are checked only by filesystem
  427. * operations (as far as semantic preservation is concerned).
  428. */
  429. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  430. {
  431. const struct cred *old;
  432. struct cred *new;
  433. int retval;
  434. new = prepare_creds();
  435. if (!new)
  436. return -ENOMEM;
  437. old = current_cred();
  438. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  439. if (retval)
  440. goto error;
  441. retval = -EPERM;
  442. if (rgid != (gid_t) -1) {
  443. if (old->gid == rgid ||
  444. old->egid == rgid ||
  445. capable(CAP_SETGID))
  446. new->gid = rgid;
  447. else
  448. goto error;
  449. }
  450. if (egid != (gid_t) -1) {
  451. if (old->gid == egid ||
  452. old->egid == egid ||
  453. old->sgid == egid ||
  454. capable(CAP_SETGID))
  455. new->egid = egid;
  456. else
  457. goto error;
  458. }
  459. if (rgid != (gid_t) -1 ||
  460. (egid != (gid_t) -1 && egid != old->gid))
  461. new->sgid = new->egid;
  462. new->fsgid = new->egid;
  463. return commit_creds(new);
  464. error:
  465. abort_creds(new);
  466. return retval;
  467. }
  468. /*
  469. * setgid() is implemented like SysV w/ SAVED_IDS
  470. *
  471. * SMP: Same implicit races as above.
  472. */
  473. SYSCALL_DEFINE1(setgid, gid_t, gid)
  474. {
  475. const struct cred *old;
  476. struct cred *new;
  477. int retval;
  478. new = prepare_creds();
  479. if (!new)
  480. return -ENOMEM;
  481. old = current_cred();
  482. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  483. if (retval)
  484. goto error;
  485. retval = -EPERM;
  486. if (capable(CAP_SETGID))
  487. new->gid = new->egid = new->sgid = new->fsgid = gid;
  488. else if (gid == old->gid || gid == old->sgid)
  489. new->egid = new->fsgid = gid;
  490. else
  491. goto error;
  492. return commit_creds(new);
  493. error:
  494. abort_creds(new);
  495. return retval;
  496. }
  497. /*
  498. * change the user struct in a credentials set to match the new UID
  499. */
  500. static int set_user(struct cred *new)
  501. {
  502. struct user_struct *new_user;
  503. new_user = alloc_uid(current_user_ns(), new->uid);
  504. if (!new_user)
  505. return -EAGAIN;
  506. if (!task_can_switch_user(new_user, current)) {
  507. free_uid(new_user);
  508. return -EINVAL;
  509. }
  510. if (atomic_read(&new_user->processes) >=
  511. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  512. new_user != INIT_USER) {
  513. free_uid(new_user);
  514. return -EAGAIN;
  515. }
  516. free_uid(new->user);
  517. new->user = new_user;
  518. return 0;
  519. }
  520. /*
  521. * Unprivileged users may change the real uid to the effective uid
  522. * or vice versa. (BSD-style)
  523. *
  524. * If you set the real uid at all, or set the effective uid to a value not
  525. * equal to the real uid, then the saved uid is set to the new effective uid.
  526. *
  527. * This makes it possible for a setuid program to completely drop its
  528. * privileges, which is often a useful assertion to make when you are doing
  529. * a security audit over a program.
  530. *
  531. * The general idea is that a program which uses just setreuid() will be
  532. * 100% compatible with BSD. A program which uses just setuid() will be
  533. * 100% compatible with POSIX with saved IDs.
  534. */
  535. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  536. {
  537. const struct cred *old;
  538. struct cred *new;
  539. int retval;
  540. new = prepare_creds();
  541. if (!new)
  542. return -ENOMEM;
  543. old = current_cred();
  544. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  545. if (retval)
  546. goto error;
  547. retval = -EPERM;
  548. if (ruid != (uid_t) -1) {
  549. new->uid = ruid;
  550. if (old->uid != ruid &&
  551. old->euid != ruid &&
  552. !capable(CAP_SETUID))
  553. goto error;
  554. }
  555. if (euid != (uid_t) -1) {
  556. new->euid = euid;
  557. if (old->uid != euid &&
  558. old->euid != euid &&
  559. old->suid != euid &&
  560. !capable(CAP_SETUID))
  561. goto error;
  562. }
  563. if (new->uid != old->uid) {
  564. retval = set_user(new);
  565. if (retval < 0)
  566. goto error;
  567. }
  568. if (ruid != (uid_t) -1 ||
  569. (euid != (uid_t) -1 && euid != old->uid))
  570. new->suid = new->euid;
  571. new->fsuid = new->euid;
  572. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  573. if (retval < 0)
  574. goto error;
  575. return commit_creds(new);
  576. error:
  577. abort_creds(new);
  578. return retval;
  579. }
  580. /*
  581. * setuid() is implemented like SysV with SAVED_IDS
  582. *
  583. * Note that SAVED_ID's is deficient in that a setuid root program
  584. * like sendmail, for example, cannot set its uid to be a normal
  585. * user and then switch back, because if you're root, setuid() sets
  586. * the saved uid too. If you don't like this, blame the bright people
  587. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  588. * will allow a root program to temporarily drop privileges and be able to
  589. * regain them by swapping the real and effective uid.
  590. */
  591. SYSCALL_DEFINE1(setuid, uid_t, uid)
  592. {
  593. const struct cred *old;
  594. struct cred *new;
  595. int retval;
  596. new = prepare_creds();
  597. if (!new)
  598. return -ENOMEM;
  599. old = current_cred();
  600. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  601. if (retval)
  602. goto error;
  603. retval = -EPERM;
  604. if (capable(CAP_SETUID)) {
  605. new->suid = new->uid = uid;
  606. if (uid != old->uid) {
  607. retval = set_user(new);
  608. if (retval < 0)
  609. goto error;
  610. }
  611. } else if (uid != old->uid && uid != new->suid) {
  612. goto error;
  613. }
  614. new->fsuid = new->euid = uid;
  615. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  616. if (retval < 0)
  617. goto error;
  618. return commit_creds(new);
  619. error:
  620. abort_creds(new);
  621. return retval;
  622. }
  623. /*
  624. * This function implements a generic ability to update ruid, euid,
  625. * and suid. This allows you to implement the 4.4 compatible seteuid().
  626. */
  627. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  628. {
  629. const struct cred *old;
  630. struct cred *new;
  631. int retval;
  632. new = prepare_creds();
  633. if (!new)
  634. return -ENOMEM;
  635. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  636. if (retval)
  637. goto error;
  638. old = current_cred();
  639. retval = -EPERM;
  640. if (!capable(CAP_SETUID)) {
  641. if (ruid != (uid_t) -1 && ruid != old->uid &&
  642. ruid != old->euid && ruid != old->suid)
  643. goto error;
  644. if (euid != (uid_t) -1 && euid != old->uid &&
  645. euid != old->euid && euid != old->suid)
  646. goto error;
  647. if (suid != (uid_t) -1 && suid != old->uid &&
  648. suid != old->euid && suid != old->suid)
  649. goto error;
  650. }
  651. if (ruid != (uid_t) -1) {
  652. new->uid = ruid;
  653. if (ruid != old->uid) {
  654. retval = set_user(new);
  655. if (retval < 0)
  656. goto error;
  657. }
  658. }
  659. if (euid != (uid_t) -1)
  660. new->euid = euid;
  661. if (suid != (uid_t) -1)
  662. new->suid = suid;
  663. new->fsuid = new->euid;
  664. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  665. if (retval < 0)
  666. goto error;
  667. return commit_creds(new);
  668. error:
  669. abort_creds(new);
  670. return retval;
  671. }
  672. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  673. {
  674. const struct cred *cred = current_cred();
  675. int retval;
  676. if (!(retval = put_user(cred->uid, ruid)) &&
  677. !(retval = put_user(cred->euid, euid)))
  678. retval = put_user(cred->suid, suid);
  679. return retval;
  680. }
  681. /*
  682. * Same as above, but for rgid, egid, sgid.
  683. */
  684. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  685. {
  686. const struct cred *old;
  687. struct cred *new;
  688. int retval;
  689. new = prepare_creds();
  690. if (!new)
  691. return -ENOMEM;
  692. old = current_cred();
  693. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  694. if (retval)
  695. goto error;
  696. retval = -EPERM;
  697. if (!capable(CAP_SETGID)) {
  698. if (rgid != (gid_t) -1 && rgid != old->gid &&
  699. rgid != old->egid && rgid != old->sgid)
  700. goto error;
  701. if (egid != (gid_t) -1 && egid != old->gid &&
  702. egid != old->egid && egid != old->sgid)
  703. goto error;
  704. if (sgid != (gid_t) -1 && sgid != old->gid &&
  705. sgid != old->egid && sgid != old->sgid)
  706. goto error;
  707. }
  708. if (rgid != (gid_t) -1)
  709. new->gid = rgid;
  710. if (egid != (gid_t) -1)
  711. new->egid = egid;
  712. if (sgid != (gid_t) -1)
  713. new->sgid = sgid;
  714. new->fsgid = new->egid;
  715. return commit_creds(new);
  716. error:
  717. abort_creds(new);
  718. return retval;
  719. }
  720. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  721. {
  722. const struct cred *cred = current_cred();
  723. int retval;
  724. if (!(retval = put_user(cred->gid, rgid)) &&
  725. !(retval = put_user(cred->egid, egid)))
  726. retval = put_user(cred->sgid, sgid);
  727. return retval;
  728. }
  729. /*
  730. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  731. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  732. * whatever uid it wants to). It normally shadows "euid", except when
  733. * explicitly set by setfsuid() or for access..
  734. */
  735. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  736. {
  737. const struct cred *old;
  738. struct cred *new;
  739. uid_t old_fsuid;
  740. new = prepare_creds();
  741. if (!new)
  742. return current_fsuid();
  743. old = current_cred();
  744. old_fsuid = old->fsuid;
  745. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
  746. goto error;
  747. if (uid == old->uid || uid == old->euid ||
  748. uid == old->suid || uid == old->fsuid ||
  749. capable(CAP_SETUID)) {
  750. if (uid != old_fsuid) {
  751. new->fsuid = uid;
  752. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  753. goto change_okay;
  754. }
  755. }
  756. error:
  757. abort_creds(new);
  758. return old_fsuid;
  759. change_okay:
  760. commit_creds(new);
  761. return old_fsuid;
  762. }
  763. /*
  764. * Samma på svenska..
  765. */
  766. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  767. {
  768. const struct cred *old;
  769. struct cred *new;
  770. gid_t old_fsgid;
  771. new = prepare_creds();
  772. if (!new)
  773. return current_fsgid();
  774. old = current_cred();
  775. old_fsgid = old->fsgid;
  776. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  777. goto error;
  778. if (gid == old->gid || gid == old->egid ||
  779. gid == old->sgid || gid == old->fsgid ||
  780. capable(CAP_SETGID)) {
  781. if (gid != old_fsgid) {
  782. new->fsgid = gid;
  783. goto change_okay;
  784. }
  785. }
  786. error:
  787. abort_creds(new);
  788. return old_fsgid;
  789. change_okay:
  790. commit_creds(new);
  791. return old_fsgid;
  792. }
  793. void do_sys_times(struct tms *tms)
  794. {
  795. struct task_cputime cputime;
  796. cputime_t cutime, cstime;
  797. thread_group_cputime(current, &cputime);
  798. spin_lock_irq(&current->sighand->siglock);
  799. cutime = current->signal->cutime;
  800. cstime = current->signal->cstime;
  801. spin_unlock_irq(&current->sighand->siglock);
  802. tms->tms_utime = cputime_to_clock_t(cputime.utime);
  803. tms->tms_stime = cputime_to_clock_t(cputime.stime);
  804. tms->tms_cutime = cputime_to_clock_t(cutime);
  805. tms->tms_cstime = cputime_to_clock_t(cstime);
  806. }
  807. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  808. {
  809. if (tbuf) {
  810. struct tms tmp;
  811. do_sys_times(&tmp);
  812. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  813. return -EFAULT;
  814. }
  815. force_successful_syscall_return();
  816. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  817. }
  818. /*
  819. * This needs some heavy checking ...
  820. * I just haven't the stomach for it. I also don't fully
  821. * understand sessions/pgrp etc. Let somebody who does explain it.
  822. *
  823. * OK, I think I have the protection semantics right.... this is really
  824. * only important on a multi-user system anyway, to make sure one user
  825. * can't send a signal to a process owned by another. -TYT, 12/12/91
  826. *
  827. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  828. * LBT 04.03.94
  829. */
  830. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  831. {
  832. struct task_struct *p;
  833. struct task_struct *group_leader = current->group_leader;
  834. struct pid *pgrp;
  835. int err;
  836. if (!pid)
  837. pid = task_pid_vnr(group_leader);
  838. if (!pgid)
  839. pgid = pid;
  840. if (pgid < 0)
  841. return -EINVAL;
  842. /* From this point forward we keep holding onto the tasklist lock
  843. * so that our parent does not change from under us. -DaveM
  844. */
  845. write_lock_irq(&tasklist_lock);
  846. err = -ESRCH;
  847. p = find_task_by_vpid(pid);
  848. if (!p)
  849. goto out;
  850. err = -EINVAL;
  851. if (!thread_group_leader(p))
  852. goto out;
  853. if (same_thread_group(p->real_parent, group_leader)) {
  854. err = -EPERM;
  855. if (task_session(p) != task_session(group_leader))
  856. goto out;
  857. err = -EACCES;
  858. if (p->did_exec)
  859. goto out;
  860. } else {
  861. err = -ESRCH;
  862. if (p != group_leader)
  863. goto out;
  864. }
  865. err = -EPERM;
  866. if (p->signal->leader)
  867. goto out;
  868. pgrp = task_pid(p);
  869. if (pgid != pid) {
  870. struct task_struct *g;
  871. pgrp = find_vpid(pgid);
  872. g = pid_task(pgrp, PIDTYPE_PGID);
  873. if (!g || task_session(g) != task_session(group_leader))
  874. goto out;
  875. }
  876. err = security_task_setpgid(p, pgid);
  877. if (err)
  878. goto out;
  879. if (task_pgrp(p) != pgrp)
  880. change_pid(p, PIDTYPE_PGID, pgrp);
  881. err = 0;
  882. out:
  883. /* All paths lead to here, thus we are safe. -DaveM */
  884. write_unlock_irq(&tasklist_lock);
  885. return err;
  886. }
  887. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  888. {
  889. struct task_struct *p;
  890. struct pid *grp;
  891. int retval;
  892. rcu_read_lock();
  893. if (!pid)
  894. grp = task_pgrp(current);
  895. else {
  896. retval = -ESRCH;
  897. p = find_task_by_vpid(pid);
  898. if (!p)
  899. goto out;
  900. grp = task_pgrp(p);
  901. if (!grp)
  902. goto out;
  903. retval = security_task_getpgid(p);
  904. if (retval)
  905. goto out;
  906. }
  907. retval = pid_vnr(grp);
  908. out:
  909. rcu_read_unlock();
  910. return retval;
  911. }
  912. #ifdef __ARCH_WANT_SYS_GETPGRP
  913. SYSCALL_DEFINE0(getpgrp)
  914. {
  915. return sys_getpgid(0);
  916. }
  917. #endif
  918. SYSCALL_DEFINE1(getsid, pid_t, pid)
  919. {
  920. struct task_struct *p;
  921. struct pid *sid;
  922. int retval;
  923. rcu_read_lock();
  924. if (!pid)
  925. sid = task_session(current);
  926. else {
  927. retval = -ESRCH;
  928. p = find_task_by_vpid(pid);
  929. if (!p)
  930. goto out;
  931. sid = task_session(p);
  932. if (!sid)
  933. goto out;
  934. retval = security_task_getsid(p);
  935. if (retval)
  936. goto out;
  937. }
  938. retval = pid_vnr(sid);
  939. out:
  940. rcu_read_unlock();
  941. return retval;
  942. }
  943. SYSCALL_DEFINE0(setsid)
  944. {
  945. struct task_struct *group_leader = current->group_leader;
  946. struct pid *sid = task_pid(group_leader);
  947. pid_t session = pid_vnr(sid);
  948. int err = -EPERM;
  949. write_lock_irq(&tasklist_lock);
  950. /* Fail if I am already a session leader */
  951. if (group_leader->signal->leader)
  952. goto out;
  953. /* Fail if a process group id already exists that equals the
  954. * proposed session id.
  955. */
  956. if (pid_task(sid, PIDTYPE_PGID))
  957. goto out;
  958. group_leader->signal->leader = 1;
  959. __set_special_pids(sid);
  960. proc_clear_tty(group_leader);
  961. err = session;
  962. out:
  963. write_unlock_irq(&tasklist_lock);
  964. if (err > 0)
  965. proc_sid_connector(group_leader);
  966. return err;
  967. }
  968. DECLARE_RWSEM(uts_sem);
  969. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  970. {
  971. int errno = 0;
  972. down_read(&uts_sem);
  973. if (copy_to_user(name, utsname(), sizeof *name))
  974. errno = -EFAULT;
  975. up_read(&uts_sem);
  976. return errno;
  977. }
  978. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  979. {
  980. int errno;
  981. char tmp[__NEW_UTS_LEN];
  982. if (!capable(CAP_SYS_ADMIN))
  983. return -EPERM;
  984. if (len < 0 || len > __NEW_UTS_LEN)
  985. return -EINVAL;
  986. down_write(&uts_sem);
  987. errno = -EFAULT;
  988. if (!copy_from_user(tmp, name, len)) {
  989. struct new_utsname *u = utsname();
  990. memcpy(u->nodename, tmp, len);
  991. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  992. errno = 0;
  993. }
  994. up_write(&uts_sem);
  995. return errno;
  996. }
  997. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  998. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  999. {
  1000. int i, errno;
  1001. struct new_utsname *u;
  1002. if (len < 0)
  1003. return -EINVAL;
  1004. down_read(&uts_sem);
  1005. u = utsname();
  1006. i = 1 + strlen(u->nodename);
  1007. if (i > len)
  1008. i = len;
  1009. errno = 0;
  1010. if (copy_to_user(name, u->nodename, i))
  1011. errno = -EFAULT;
  1012. up_read(&uts_sem);
  1013. return errno;
  1014. }
  1015. #endif
  1016. /*
  1017. * Only setdomainname; getdomainname can be implemented by calling
  1018. * uname()
  1019. */
  1020. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1021. {
  1022. int errno;
  1023. char tmp[__NEW_UTS_LEN];
  1024. if (!capable(CAP_SYS_ADMIN))
  1025. return -EPERM;
  1026. if (len < 0 || len > __NEW_UTS_LEN)
  1027. return -EINVAL;
  1028. down_write(&uts_sem);
  1029. errno = -EFAULT;
  1030. if (!copy_from_user(tmp, name, len)) {
  1031. struct new_utsname *u = utsname();
  1032. memcpy(u->domainname, tmp, len);
  1033. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1034. errno = 0;
  1035. }
  1036. up_write(&uts_sem);
  1037. return errno;
  1038. }
  1039. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1040. {
  1041. if (resource >= RLIM_NLIMITS)
  1042. return -EINVAL;
  1043. else {
  1044. struct rlimit value;
  1045. task_lock(current->group_leader);
  1046. value = current->signal->rlim[resource];
  1047. task_unlock(current->group_leader);
  1048. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1049. }
  1050. }
  1051. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1052. /*
  1053. * Back compatibility for getrlimit. Needed for some apps.
  1054. */
  1055. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1056. struct rlimit __user *, rlim)
  1057. {
  1058. struct rlimit x;
  1059. if (resource >= RLIM_NLIMITS)
  1060. return -EINVAL;
  1061. task_lock(current->group_leader);
  1062. x = current->signal->rlim[resource];
  1063. task_unlock(current->group_leader);
  1064. if (x.rlim_cur > 0x7FFFFFFF)
  1065. x.rlim_cur = 0x7FFFFFFF;
  1066. if (x.rlim_max > 0x7FFFFFFF)
  1067. x.rlim_max = 0x7FFFFFFF;
  1068. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1069. }
  1070. #endif
  1071. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1072. {
  1073. struct rlimit new_rlim, *old_rlim;
  1074. int retval;
  1075. if (resource >= RLIM_NLIMITS)
  1076. return -EINVAL;
  1077. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1078. return -EFAULT;
  1079. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1080. return -EINVAL;
  1081. old_rlim = current->signal->rlim + resource;
  1082. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1083. !capable(CAP_SYS_RESOURCE))
  1084. return -EPERM;
  1085. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1086. return -EPERM;
  1087. retval = security_task_setrlimit(resource, &new_rlim);
  1088. if (retval)
  1089. return retval;
  1090. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1091. /*
  1092. * The caller is asking for an immediate RLIMIT_CPU
  1093. * expiry. But we use the zero value to mean "it was
  1094. * never set". So let's cheat and make it one second
  1095. * instead
  1096. */
  1097. new_rlim.rlim_cur = 1;
  1098. }
  1099. task_lock(current->group_leader);
  1100. *old_rlim = new_rlim;
  1101. task_unlock(current->group_leader);
  1102. if (resource != RLIMIT_CPU)
  1103. goto out;
  1104. /*
  1105. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1106. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1107. * very long-standing error, and fixing it now risks breakage of
  1108. * applications, so we live with it
  1109. */
  1110. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1111. goto out;
  1112. update_rlimit_cpu(new_rlim.rlim_cur);
  1113. out:
  1114. return 0;
  1115. }
  1116. /*
  1117. * It would make sense to put struct rusage in the task_struct,
  1118. * except that would make the task_struct be *really big*. After
  1119. * task_struct gets moved into malloc'ed memory, it would
  1120. * make sense to do this. It will make moving the rest of the information
  1121. * a lot simpler! (Which we're not doing right now because we're not
  1122. * measuring them yet).
  1123. *
  1124. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1125. * races with threads incrementing their own counters. But since word
  1126. * reads are atomic, we either get new values or old values and we don't
  1127. * care which for the sums. We always take the siglock to protect reading
  1128. * the c* fields from p->signal from races with exit.c updating those
  1129. * fields when reaping, so a sample either gets all the additions of a
  1130. * given child after it's reaped, or none so this sample is before reaping.
  1131. *
  1132. * Locking:
  1133. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1134. * for the cases current multithreaded, non-current single threaded
  1135. * non-current multithreaded. Thread traversal is now safe with
  1136. * the siglock held.
  1137. * Strictly speaking, we donot need to take the siglock if we are current and
  1138. * single threaded, as no one else can take our signal_struct away, no one
  1139. * else can reap the children to update signal->c* counters, and no one else
  1140. * can race with the signal-> fields. If we do not take any lock, the
  1141. * signal-> fields could be read out of order while another thread was just
  1142. * exiting. So we should place a read memory barrier when we avoid the lock.
  1143. * On the writer side, write memory barrier is implied in __exit_signal
  1144. * as __exit_signal releases the siglock spinlock after updating the signal->
  1145. * fields. But we don't do this yet to keep things simple.
  1146. *
  1147. */
  1148. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1149. {
  1150. r->ru_nvcsw += t->nvcsw;
  1151. r->ru_nivcsw += t->nivcsw;
  1152. r->ru_minflt += t->min_flt;
  1153. r->ru_majflt += t->maj_flt;
  1154. r->ru_inblock += task_io_get_inblock(t);
  1155. r->ru_oublock += task_io_get_oublock(t);
  1156. }
  1157. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1158. {
  1159. struct task_struct *t;
  1160. unsigned long flags;
  1161. cputime_t utime, stime;
  1162. struct task_cputime cputime;
  1163. unsigned long maxrss = 0;
  1164. memset((char *) r, 0, sizeof *r);
  1165. utime = stime = cputime_zero;
  1166. if (who == RUSAGE_THREAD) {
  1167. utime = task_utime(current);
  1168. stime = task_stime(current);
  1169. accumulate_thread_rusage(p, r);
  1170. maxrss = p->signal->maxrss;
  1171. goto out;
  1172. }
  1173. if (!lock_task_sighand(p, &flags))
  1174. return;
  1175. switch (who) {
  1176. case RUSAGE_BOTH:
  1177. case RUSAGE_CHILDREN:
  1178. utime = p->signal->cutime;
  1179. stime = p->signal->cstime;
  1180. r->ru_nvcsw = p->signal->cnvcsw;
  1181. r->ru_nivcsw = p->signal->cnivcsw;
  1182. r->ru_minflt = p->signal->cmin_flt;
  1183. r->ru_majflt = p->signal->cmaj_flt;
  1184. r->ru_inblock = p->signal->cinblock;
  1185. r->ru_oublock = p->signal->coublock;
  1186. maxrss = p->signal->cmaxrss;
  1187. if (who == RUSAGE_CHILDREN)
  1188. break;
  1189. case RUSAGE_SELF:
  1190. thread_group_cputime(p, &cputime);
  1191. utime = cputime_add(utime, cputime.utime);
  1192. stime = cputime_add(stime, cputime.stime);
  1193. r->ru_nvcsw += p->signal->nvcsw;
  1194. r->ru_nivcsw += p->signal->nivcsw;
  1195. r->ru_minflt += p->signal->min_flt;
  1196. r->ru_majflt += p->signal->maj_flt;
  1197. r->ru_inblock += p->signal->inblock;
  1198. r->ru_oublock += p->signal->oublock;
  1199. if (maxrss < p->signal->maxrss)
  1200. maxrss = p->signal->maxrss;
  1201. t = p;
  1202. do {
  1203. accumulate_thread_rusage(t, r);
  1204. t = next_thread(t);
  1205. } while (t != p);
  1206. break;
  1207. default:
  1208. BUG();
  1209. }
  1210. unlock_task_sighand(p, &flags);
  1211. out:
  1212. cputime_to_timeval(utime, &r->ru_utime);
  1213. cputime_to_timeval(stime, &r->ru_stime);
  1214. if (who != RUSAGE_CHILDREN) {
  1215. struct mm_struct *mm = get_task_mm(p);
  1216. if (mm) {
  1217. setmax_mm_hiwater_rss(&maxrss, mm);
  1218. mmput(mm);
  1219. }
  1220. }
  1221. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1222. }
  1223. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1224. {
  1225. struct rusage r;
  1226. k_getrusage(p, who, &r);
  1227. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1228. }
  1229. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1230. {
  1231. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1232. who != RUSAGE_THREAD)
  1233. return -EINVAL;
  1234. return getrusage(current, who, ru);
  1235. }
  1236. SYSCALL_DEFINE1(umask, int, mask)
  1237. {
  1238. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1239. return mask;
  1240. }
  1241. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1242. unsigned long, arg4, unsigned long, arg5)
  1243. {
  1244. struct task_struct *me = current;
  1245. unsigned char comm[sizeof(me->comm)];
  1246. long error;
  1247. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1248. if (error != -ENOSYS)
  1249. return error;
  1250. error = 0;
  1251. switch (option) {
  1252. case PR_SET_PDEATHSIG:
  1253. if (!valid_signal(arg2)) {
  1254. error = -EINVAL;
  1255. break;
  1256. }
  1257. me->pdeath_signal = arg2;
  1258. error = 0;
  1259. break;
  1260. case PR_GET_PDEATHSIG:
  1261. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1262. break;
  1263. case PR_GET_DUMPABLE:
  1264. error = get_dumpable(me->mm);
  1265. break;
  1266. case PR_SET_DUMPABLE:
  1267. if (arg2 < 0 || arg2 > 1) {
  1268. error = -EINVAL;
  1269. break;
  1270. }
  1271. set_dumpable(me->mm, arg2);
  1272. error = 0;
  1273. break;
  1274. case PR_SET_UNALIGN:
  1275. error = SET_UNALIGN_CTL(me, arg2);
  1276. break;
  1277. case PR_GET_UNALIGN:
  1278. error = GET_UNALIGN_CTL(me, arg2);
  1279. break;
  1280. case PR_SET_FPEMU:
  1281. error = SET_FPEMU_CTL(me, arg2);
  1282. break;
  1283. case PR_GET_FPEMU:
  1284. error = GET_FPEMU_CTL(me, arg2);
  1285. break;
  1286. case PR_SET_FPEXC:
  1287. error = SET_FPEXC_CTL(me, arg2);
  1288. break;
  1289. case PR_GET_FPEXC:
  1290. error = GET_FPEXC_CTL(me, arg2);
  1291. break;
  1292. case PR_GET_TIMING:
  1293. error = PR_TIMING_STATISTICAL;
  1294. break;
  1295. case PR_SET_TIMING:
  1296. if (arg2 != PR_TIMING_STATISTICAL)
  1297. error = -EINVAL;
  1298. else
  1299. error = 0;
  1300. break;
  1301. case PR_SET_NAME:
  1302. comm[sizeof(me->comm)-1] = 0;
  1303. if (strncpy_from_user(comm, (char __user *)arg2,
  1304. sizeof(me->comm) - 1) < 0)
  1305. return -EFAULT;
  1306. set_task_comm(me, comm);
  1307. return 0;
  1308. case PR_GET_NAME:
  1309. get_task_comm(comm, me);
  1310. if (copy_to_user((char __user *)arg2, comm,
  1311. sizeof(comm)))
  1312. return -EFAULT;
  1313. return 0;
  1314. case PR_GET_ENDIAN:
  1315. error = GET_ENDIAN(me, arg2);
  1316. break;
  1317. case PR_SET_ENDIAN:
  1318. error = SET_ENDIAN(me, arg2);
  1319. break;
  1320. case PR_GET_SECCOMP:
  1321. error = prctl_get_seccomp();
  1322. break;
  1323. case PR_SET_SECCOMP:
  1324. error = prctl_set_seccomp(arg2);
  1325. break;
  1326. case PR_GET_TSC:
  1327. error = GET_TSC_CTL(arg2);
  1328. break;
  1329. case PR_SET_TSC:
  1330. error = SET_TSC_CTL(arg2);
  1331. break;
  1332. case PR_TASK_PERF_EVENTS_DISABLE:
  1333. error = perf_event_task_disable();
  1334. break;
  1335. case PR_TASK_PERF_EVENTS_ENABLE:
  1336. error = perf_event_task_enable();
  1337. break;
  1338. case PR_GET_TIMERSLACK:
  1339. error = current->timer_slack_ns;
  1340. break;
  1341. case PR_SET_TIMERSLACK:
  1342. if (arg2 <= 0)
  1343. current->timer_slack_ns =
  1344. current->default_timer_slack_ns;
  1345. else
  1346. current->timer_slack_ns = arg2;
  1347. error = 0;
  1348. break;
  1349. case PR_MCE_KILL:
  1350. if (arg4 | arg5)
  1351. return -EINVAL;
  1352. switch (arg2) {
  1353. case 0:
  1354. if (arg3 != 0)
  1355. return -EINVAL;
  1356. current->flags &= ~PF_MCE_PROCESS;
  1357. break;
  1358. case 1:
  1359. current->flags |= PF_MCE_PROCESS;
  1360. if (arg3 != 0)
  1361. current->flags |= PF_MCE_EARLY;
  1362. else
  1363. current->flags &= ~PF_MCE_EARLY;
  1364. break;
  1365. default:
  1366. return -EINVAL;
  1367. }
  1368. error = 0;
  1369. break;
  1370. default:
  1371. error = -EINVAL;
  1372. break;
  1373. }
  1374. return error;
  1375. }
  1376. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1377. struct getcpu_cache __user *, unused)
  1378. {
  1379. int err = 0;
  1380. int cpu = raw_smp_processor_id();
  1381. if (cpup)
  1382. err |= put_user(cpu, cpup);
  1383. if (nodep)
  1384. err |= put_user(cpu_to_node(cpu), nodep);
  1385. return err ? -EFAULT : 0;
  1386. }
  1387. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1388. static void argv_cleanup(char **argv, char **envp)
  1389. {
  1390. argv_free(argv);
  1391. }
  1392. /**
  1393. * orderly_poweroff - Trigger an orderly system poweroff
  1394. * @force: force poweroff if command execution fails
  1395. *
  1396. * This may be called from any context to trigger a system shutdown.
  1397. * If the orderly shutdown fails, it will force an immediate shutdown.
  1398. */
  1399. int orderly_poweroff(bool force)
  1400. {
  1401. int argc;
  1402. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1403. static char *envp[] = {
  1404. "HOME=/",
  1405. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1406. NULL
  1407. };
  1408. int ret = -ENOMEM;
  1409. struct subprocess_info *info;
  1410. if (argv == NULL) {
  1411. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1412. __func__, poweroff_cmd);
  1413. goto out;
  1414. }
  1415. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1416. if (info == NULL) {
  1417. argv_free(argv);
  1418. goto out;
  1419. }
  1420. call_usermodehelper_setcleanup(info, argv_cleanup);
  1421. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1422. out:
  1423. if (ret && force) {
  1424. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1425. "forcing the issue\n");
  1426. /* I guess this should try to kick off some daemon to
  1427. sync and poweroff asap. Or not even bother syncing
  1428. if we're doing an emergency shutdown? */
  1429. emergency_sync();
  1430. kernel_power_off();
  1431. }
  1432. return ret;
  1433. }
  1434. EXPORT_SYMBOL_GPL(orderly_poweroff);