sys.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  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/notifier.h>
  11. #include <linux/reboot.h>
  12. #include <linux/prctl.h>
  13. #include <linux/highuid.h>
  14. #include <linux/fs.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/ptrace.h>
  36. #include <linux/fs_struct.h>
  37. #include <linux/compat.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/kprobes.h>
  40. #include <linux/user_namespace.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/io.h>
  43. #include <asm/unistd.h>
  44. #ifndef SET_UNALIGN_CTL
  45. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  46. #endif
  47. #ifndef GET_UNALIGN_CTL
  48. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  49. #endif
  50. #ifndef SET_FPEMU_CTL
  51. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  52. #endif
  53. #ifndef GET_FPEMU_CTL
  54. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  55. #endif
  56. #ifndef SET_FPEXC_CTL
  57. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  58. #endif
  59. #ifndef GET_FPEXC_CTL
  60. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  61. #endif
  62. #ifndef GET_ENDIAN
  63. # define GET_ENDIAN(a,b) (-EINVAL)
  64. #endif
  65. #ifndef SET_ENDIAN
  66. # define SET_ENDIAN(a,b) (-EINVAL)
  67. #endif
  68. #ifndef GET_TSC_CTL
  69. # define GET_TSC_CTL(a) (-EINVAL)
  70. #endif
  71. #ifndef SET_TSC_CTL
  72. # define SET_TSC_CTL(a) (-EINVAL)
  73. #endif
  74. /*
  75. * this is where the system-wide overflow UID and GID are defined, for
  76. * architectures that now have 32-bit UID/GID but didn't in the past
  77. */
  78. int overflowuid = DEFAULT_OVERFLOWUID;
  79. int overflowgid = DEFAULT_OVERFLOWGID;
  80. #ifdef CONFIG_UID16
  81. EXPORT_SYMBOL(overflowuid);
  82. EXPORT_SYMBOL(overflowgid);
  83. #endif
  84. /*
  85. * the same as above, but for filesystems which can only store a 16-bit
  86. * UID and GID. as such, this is needed on all architectures
  87. */
  88. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  89. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  90. EXPORT_SYMBOL(fs_overflowuid);
  91. EXPORT_SYMBOL(fs_overflowgid);
  92. /*
  93. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  94. */
  95. int C_A_D = 1;
  96. struct pid *cad_pid;
  97. EXPORT_SYMBOL(cad_pid);
  98. /*
  99. * If set, this is used for preparing the system to power off.
  100. */
  101. void (*pm_power_off_prepare)(void);
  102. /*
  103. * set the priority of a task
  104. * - the caller must hold the RCU read lock
  105. */
  106. static int set_one_prio(struct task_struct *p, int niceval, int error)
  107. {
  108. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  109. int no_nice;
  110. if (pcred->uid != cred->euid &&
  111. pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
  112. error = -EPERM;
  113. goto out;
  114. }
  115. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  116. error = -EACCES;
  117. goto out;
  118. }
  119. no_nice = security_task_setnice(p, niceval);
  120. if (no_nice) {
  121. error = no_nice;
  122. goto out;
  123. }
  124. if (error == -ESRCH)
  125. error = 0;
  126. set_user_nice(p, niceval);
  127. out:
  128. return error;
  129. }
  130. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  131. {
  132. struct task_struct *g, *p;
  133. struct user_struct *user;
  134. const struct cred *cred = current_cred();
  135. int error = -EINVAL;
  136. struct pid *pgrp;
  137. if (which > PRIO_USER || which < PRIO_PROCESS)
  138. goto out;
  139. /* normalize: avoid signed division (rounding problems) */
  140. error = -ESRCH;
  141. if (niceval < -20)
  142. niceval = -20;
  143. if (niceval > 19)
  144. niceval = 19;
  145. rcu_read_lock();
  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. rcu_read_unlock();
  183. out:
  184. return error;
  185. }
  186. /*
  187. * Ugh. To avoid negative return values, "getpriority()" will
  188. * not return the normal nice-value, but a negated value that
  189. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  190. * to stay compatible.
  191. */
  192. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  193. {
  194. struct task_struct *g, *p;
  195. struct user_struct *user;
  196. const struct cred *cred = current_cred();
  197. long niceval, retval = -ESRCH;
  198. struct pid *pgrp;
  199. if (which > PRIO_USER || which < PRIO_PROCESS)
  200. return -EINVAL;
  201. read_lock(&tasklist_lock);
  202. switch (which) {
  203. case PRIO_PROCESS:
  204. if (who)
  205. p = find_task_by_vpid(who);
  206. else
  207. p = current;
  208. if (p) {
  209. niceval = 20 - task_nice(p);
  210. if (niceval > retval)
  211. retval = niceval;
  212. }
  213. break;
  214. case PRIO_PGRP:
  215. if (who)
  216. pgrp = find_vpid(who);
  217. else
  218. pgrp = task_pgrp(current);
  219. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  220. niceval = 20 - task_nice(p);
  221. if (niceval > retval)
  222. retval = niceval;
  223. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  224. break;
  225. case PRIO_USER:
  226. user = (struct user_struct *) cred->user;
  227. if (!who)
  228. who = cred->uid;
  229. else if ((who != cred->uid) &&
  230. !(user = find_user(who)))
  231. goto out_unlock; /* No processes for this user */
  232. do_each_thread(g, p) {
  233. if (__task_cred(p)->uid == who) {
  234. niceval = 20 - task_nice(p);
  235. if (niceval > retval)
  236. retval = niceval;
  237. }
  238. } while_each_thread(g, p);
  239. if (who != cred->uid)
  240. free_uid(user); /* for find_user() */
  241. break;
  242. }
  243. out_unlock:
  244. read_unlock(&tasklist_lock);
  245. return retval;
  246. }
  247. /**
  248. * emergency_restart - reboot the system
  249. *
  250. * Without shutting down any hardware or taking any locks
  251. * reboot the system. This is called when we know we are in
  252. * trouble so this is our best effort to reboot. This is
  253. * safe to call in interrupt context.
  254. */
  255. void emergency_restart(void)
  256. {
  257. machine_emergency_restart();
  258. }
  259. EXPORT_SYMBOL_GPL(emergency_restart);
  260. void kernel_restart_prepare(char *cmd)
  261. {
  262. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  263. system_state = SYSTEM_RESTART;
  264. device_shutdown();
  265. sysdev_shutdown();
  266. }
  267. /**
  268. * kernel_restart - reboot the system
  269. * @cmd: pointer to buffer containing command to execute for restart
  270. * or %NULL
  271. *
  272. * Shutdown everything and perform a clean reboot.
  273. * This is not safe to call in interrupt context.
  274. */
  275. void kernel_restart(char *cmd)
  276. {
  277. kernel_restart_prepare(cmd);
  278. if (!cmd)
  279. printk(KERN_EMERG "Restarting system.\n");
  280. else
  281. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  282. machine_restart(cmd);
  283. }
  284. EXPORT_SYMBOL_GPL(kernel_restart);
  285. static void kernel_shutdown_prepare(enum system_states state)
  286. {
  287. blocking_notifier_call_chain(&reboot_notifier_list,
  288. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  289. system_state = state;
  290. device_shutdown();
  291. }
  292. /**
  293. * kernel_halt - halt the system
  294. *
  295. * Shutdown everything and perform a clean system halt.
  296. */
  297. void kernel_halt(void)
  298. {
  299. kernel_shutdown_prepare(SYSTEM_HALT);
  300. sysdev_shutdown();
  301. printk(KERN_EMERG "System halted.\n");
  302. machine_halt();
  303. }
  304. EXPORT_SYMBOL_GPL(kernel_halt);
  305. /**
  306. * kernel_power_off - power_off the system
  307. *
  308. * Shutdown everything and perform a clean system power_off.
  309. */
  310. void kernel_power_off(void)
  311. {
  312. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  313. if (pm_power_off_prepare)
  314. pm_power_off_prepare();
  315. disable_nonboot_cpus();
  316. sysdev_shutdown();
  317. printk(KERN_EMERG "Power down.\n");
  318. machine_power_off();
  319. }
  320. EXPORT_SYMBOL_GPL(kernel_power_off);
  321. static DEFINE_MUTEX(reboot_mutex);
  322. /*
  323. * Reboot system call: for obvious reasons only root may call it,
  324. * and even root needs to set up some magic numbers in the registers
  325. * so that some mistake won't make this reboot the whole machine.
  326. * You can also set the meaning of the ctrl-alt-del-key here.
  327. *
  328. * reboot doesn't sync: do that yourself before calling this.
  329. */
  330. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  331. void __user *, arg)
  332. {
  333. char buffer[256];
  334. int ret = 0;
  335. /* We only trust the superuser with rebooting the system. */
  336. if (!capable(CAP_SYS_BOOT))
  337. return -EPERM;
  338. /* For safety, we require "magic" arguments. */
  339. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  340. (magic2 != LINUX_REBOOT_MAGIC2 &&
  341. magic2 != LINUX_REBOOT_MAGIC2A &&
  342. magic2 != LINUX_REBOOT_MAGIC2B &&
  343. magic2 != LINUX_REBOOT_MAGIC2C))
  344. return -EINVAL;
  345. /* Instead of trying to make the power_off code look like
  346. * halt when pm_power_off is not set do it the easy way.
  347. */
  348. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  349. cmd = LINUX_REBOOT_CMD_HALT;
  350. mutex_lock(&reboot_mutex);
  351. switch (cmd) {
  352. case LINUX_REBOOT_CMD_RESTART:
  353. kernel_restart(NULL);
  354. break;
  355. case LINUX_REBOOT_CMD_CAD_ON:
  356. C_A_D = 1;
  357. break;
  358. case LINUX_REBOOT_CMD_CAD_OFF:
  359. C_A_D = 0;
  360. break;
  361. case LINUX_REBOOT_CMD_HALT:
  362. kernel_halt();
  363. do_exit(0);
  364. panic("cannot halt");
  365. case LINUX_REBOOT_CMD_POWER_OFF:
  366. kernel_power_off();
  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. ret = -EFAULT;
  372. break;
  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. mutex_unlock(&reboot_mutex);
  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. cputime_t tgutime, tgstime, cutime, cstime;
  796. spin_lock_irq(&current->sighand->siglock);
  797. thread_group_times(current, &tgutime, &tgstime);
  798. cutime = current->signal->cutime;
  799. cstime = current->signal->cstime;
  800. spin_unlock_irq(&current->sighand->siglock);
  801. tms->tms_utime = cputime_to_clock_t(tgutime);
  802. tms->tms_stime = cputime_to_clock_t(tgstime);
  803. tms->tms_cutime = cputime_to_clock_t(cutime);
  804. tms->tms_cstime = cputime_to_clock_t(cstime);
  805. }
  806. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  807. {
  808. if (tbuf) {
  809. struct tms tmp;
  810. do_sys_times(&tmp);
  811. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  812. return -EFAULT;
  813. }
  814. force_successful_syscall_return();
  815. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  816. }
  817. /*
  818. * This needs some heavy checking ...
  819. * I just haven't the stomach for it. I also don't fully
  820. * understand sessions/pgrp etc. Let somebody who does explain it.
  821. *
  822. * OK, I think I have the protection semantics right.... this is really
  823. * only important on a multi-user system anyway, to make sure one user
  824. * can't send a signal to a process owned by another. -TYT, 12/12/91
  825. *
  826. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  827. * LBT 04.03.94
  828. */
  829. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  830. {
  831. struct task_struct *p;
  832. struct task_struct *group_leader = current->group_leader;
  833. struct pid *pgrp;
  834. int err;
  835. if (!pid)
  836. pid = task_pid_vnr(group_leader);
  837. if (!pgid)
  838. pgid = pid;
  839. if (pgid < 0)
  840. return -EINVAL;
  841. /* From this point forward we keep holding onto the tasklist lock
  842. * so that our parent does not change from under us. -DaveM
  843. */
  844. write_lock_irq(&tasklist_lock);
  845. err = -ESRCH;
  846. p = find_task_by_vpid(pid);
  847. if (!p)
  848. goto out;
  849. err = -EINVAL;
  850. if (!thread_group_leader(p))
  851. goto out;
  852. if (same_thread_group(p->real_parent, group_leader)) {
  853. err = -EPERM;
  854. if (task_session(p) != task_session(group_leader))
  855. goto out;
  856. err = -EACCES;
  857. if (p->did_exec)
  858. goto out;
  859. } else {
  860. err = -ESRCH;
  861. if (p != group_leader)
  862. goto out;
  863. }
  864. err = -EPERM;
  865. if (p->signal->leader)
  866. goto out;
  867. pgrp = task_pid(p);
  868. if (pgid != pid) {
  869. struct task_struct *g;
  870. pgrp = find_vpid(pgid);
  871. g = pid_task(pgrp, PIDTYPE_PGID);
  872. if (!g || task_session(g) != task_session(group_leader))
  873. goto out;
  874. }
  875. err = security_task_setpgid(p, pgid);
  876. if (err)
  877. goto out;
  878. if (task_pgrp(p) != pgrp)
  879. change_pid(p, PIDTYPE_PGID, pgrp);
  880. err = 0;
  881. out:
  882. /* All paths lead to here, thus we are safe. -DaveM */
  883. write_unlock_irq(&tasklist_lock);
  884. return err;
  885. }
  886. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  887. {
  888. struct task_struct *p;
  889. struct pid *grp;
  890. int retval;
  891. rcu_read_lock();
  892. if (!pid)
  893. grp = task_pgrp(current);
  894. else {
  895. retval = -ESRCH;
  896. p = find_task_by_vpid(pid);
  897. if (!p)
  898. goto out;
  899. grp = task_pgrp(p);
  900. if (!grp)
  901. goto out;
  902. retval = security_task_getpgid(p);
  903. if (retval)
  904. goto out;
  905. }
  906. retval = pid_vnr(grp);
  907. out:
  908. rcu_read_unlock();
  909. return retval;
  910. }
  911. #ifdef __ARCH_WANT_SYS_GETPGRP
  912. SYSCALL_DEFINE0(getpgrp)
  913. {
  914. return sys_getpgid(0);
  915. }
  916. #endif
  917. SYSCALL_DEFINE1(getsid, pid_t, pid)
  918. {
  919. struct task_struct *p;
  920. struct pid *sid;
  921. int retval;
  922. rcu_read_lock();
  923. if (!pid)
  924. sid = task_session(current);
  925. else {
  926. retval = -ESRCH;
  927. p = find_task_by_vpid(pid);
  928. if (!p)
  929. goto out;
  930. sid = task_session(p);
  931. if (!sid)
  932. goto out;
  933. retval = security_task_getsid(p);
  934. if (retval)
  935. goto out;
  936. }
  937. retval = pid_vnr(sid);
  938. out:
  939. rcu_read_unlock();
  940. return retval;
  941. }
  942. SYSCALL_DEFINE0(setsid)
  943. {
  944. struct task_struct *group_leader = current->group_leader;
  945. struct pid *sid = task_pid(group_leader);
  946. pid_t session = pid_vnr(sid);
  947. int err = -EPERM;
  948. write_lock_irq(&tasklist_lock);
  949. /* Fail if I am already a session leader */
  950. if (group_leader->signal->leader)
  951. goto out;
  952. /* Fail if a process group id already exists that equals the
  953. * proposed session id.
  954. */
  955. if (pid_task(sid, PIDTYPE_PGID))
  956. goto out;
  957. group_leader->signal->leader = 1;
  958. __set_special_pids(sid);
  959. proc_clear_tty(group_leader);
  960. err = session;
  961. out:
  962. write_unlock_irq(&tasklist_lock);
  963. if (err > 0)
  964. proc_sid_connector(group_leader);
  965. return err;
  966. }
  967. DECLARE_RWSEM(uts_sem);
  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. return errno;
  976. }
  977. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  978. {
  979. int errno;
  980. char tmp[__NEW_UTS_LEN];
  981. if (!capable(CAP_SYS_ADMIN))
  982. return -EPERM;
  983. if (len < 0 || len > __NEW_UTS_LEN)
  984. return -EINVAL;
  985. down_write(&uts_sem);
  986. errno = -EFAULT;
  987. if (!copy_from_user(tmp, name, len)) {
  988. struct new_utsname *u = utsname();
  989. memcpy(u->nodename, tmp, len);
  990. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  991. errno = 0;
  992. }
  993. up_write(&uts_sem);
  994. return errno;
  995. }
  996. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  997. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  998. {
  999. int i, errno;
  1000. struct new_utsname *u;
  1001. if (len < 0)
  1002. return -EINVAL;
  1003. down_read(&uts_sem);
  1004. u = utsname();
  1005. i = 1 + strlen(u->nodename);
  1006. if (i > len)
  1007. i = len;
  1008. errno = 0;
  1009. if (copy_to_user(name, u->nodename, i))
  1010. errno = -EFAULT;
  1011. up_read(&uts_sem);
  1012. return errno;
  1013. }
  1014. #endif
  1015. /*
  1016. * Only setdomainname; getdomainname can be implemented by calling
  1017. * uname()
  1018. */
  1019. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1020. {
  1021. int errno;
  1022. char tmp[__NEW_UTS_LEN];
  1023. if (!capable(CAP_SYS_ADMIN))
  1024. return -EPERM;
  1025. if (len < 0 || len > __NEW_UTS_LEN)
  1026. return -EINVAL;
  1027. down_write(&uts_sem);
  1028. errno = -EFAULT;
  1029. if (!copy_from_user(tmp, name, len)) {
  1030. struct new_utsname *u = utsname();
  1031. memcpy(u->domainname, tmp, len);
  1032. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1033. errno = 0;
  1034. }
  1035. up_write(&uts_sem);
  1036. return errno;
  1037. }
  1038. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1039. {
  1040. if (resource >= RLIM_NLIMITS)
  1041. return -EINVAL;
  1042. else {
  1043. struct rlimit value;
  1044. task_lock(current->group_leader);
  1045. value = current->signal->rlim[resource];
  1046. task_unlock(current->group_leader);
  1047. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1048. }
  1049. }
  1050. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1051. /*
  1052. * Back compatibility for getrlimit. Needed for some apps.
  1053. */
  1054. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1055. struct rlimit __user *, rlim)
  1056. {
  1057. struct rlimit x;
  1058. if (resource >= RLIM_NLIMITS)
  1059. return -EINVAL;
  1060. task_lock(current->group_leader);
  1061. x = current->signal->rlim[resource];
  1062. task_unlock(current->group_leader);
  1063. if (x.rlim_cur > 0x7FFFFFFF)
  1064. x.rlim_cur = 0x7FFFFFFF;
  1065. if (x.rlim_max > 0x7FFFFFFF)
  1066. x.rlim_max = 0x7FFFFFFF;
  1067. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1068. }
  1069. #endif
  1070. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1071. {
  1072. struct rlimit new_rlim, *old_rlim;
  1073. int retval;
  1074. if (resource >= RLIM_NLIMITS)
  1075. return -EINVAL;
  1076. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1077. return -EFAULT;
  1078. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1079. return -EINVAL;
  1080. old_rlim = current->signal->rlim + resource;
  1081. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1082. !capable(CAP_SYS_RESOURCE))
  1083. return -EPERM;
  1084. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1085. return -EPERM;
  1086. retval = security_task_setrlimit(resource, &new_rlim);
  1087. if (retval)
  1088. return retval;
  1089. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1090. /*
  1091. * The caller is asking for an immediate RLIMIT_CPU
  1092. * expiry. But we use the zero value to mean "it was
  1093. * never set". So let's cheat and make it one second
  1094. * instead
  1095. */
  1096. new_rlim.rlim_cur = 1;
  1097. }
  1098. task_lock(current->group_leader);
  1099. *old_rlim = new_rlim;
  1100. task_unlock(current->group_leader);
  1101. if (resource != RLIMIT_CPU)
  1102. goto out;
  1103. /*
  1104. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1105. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1106. * very long-standing error, and fixing it now risks breakage of
  1107. * applications, so we live with it
  1108. */
  1109. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1110. goto out;
  1111. update_rlimit_cpu(new_rlim.rlim_cur);
  1112. out:
  1113. return 0;
  1114. }
  1115. /*
  1116. * It would make sense to put struct rusage in the task_struct,
  1117. * except that would make the task_struct be *really big*. After
  1118. * task_struct gets moved into malloc'ed memory, it would
  1119. * make sense to do this. It will make moving the rest of the information
  1120. * a lot simpler! (Which we're not doing right now because we're not
  1121. * measuring them yet).
  1122. *
  1123. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1124. * races with threads incrementing their own counters. But since word
  1125. * reads are atomic, we either get new values or old values and we don't
  1126. * care which for the sums. We always take the siglock to protect reading
  1127. * the c* fields from p->signal from races with exit.c updating those
  1128. * fields when reaping, so a sample either gets all the additions of a
  1129. * given child after it's reaped, or none so this sample is before reaping.
  1130. *
  1131. * Locking:
  1132. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1133. * for the cases current multithreaded, non-current single threaded
  1134. * non-current multithreaded. Thread traversal is now safe with
  1135. * the siglock held.
  1136. * Strictly speaking, we donot need to take the siglock if we are current and
  1137. * single threaded, as no one else can take our signal_struct away, no one
  1138. * else can reap the children to update signal->c* counters, and no one else
  1139. * can race with the signal-> fields. If we do not take any lock, the
  1140. * signal-> fields could be read out of order while another thread was just
  1141. * exiting. So we should place a read memory barrier when we avoid the lock.
  1142. * On the writer side, write memory barrier is implied in __exit_signal
  1143. * as __exit_signal releases the siglock spinlock after updating the signal->
  1144. * fields. But we don't do this yet to keep things simple.
  1145. *
  1146. */
  1147. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1148. {
  1149. r->ru_nvcsw += t->nvcsw;
  1150. r->ru_nivcsw += t->nivcsw;
  1151. r->ru_minflt += t->min_flt;
  1152. r->ru_majflt += t->maj_flt;
  1153. r->ru_inblock += task_io_get_inblock(t);
  1154. r->ru_oublock += task_io_get_oublock(t);
  1155. }
  1156. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1157. {
  1158. struct task_struct *t;
  1159. unsigned long flags;
  1160. cputime_t tgutime, tgstime, utime, stime;
  1161. unsigned long maxrss = 0;
  1162. memset((char *) r, 0, sizeof *r);
  1163. utime = stime = cputime_zero;
  1164. if (who == RUSAGE_THREAD) {
  1165. task_times(current, &utime, &stime);
  1166. accumulate_thread_rusage(p, r);
  1167. maxrss = p->signal->maxrss;
  1168. goto out;
  1169. }
  1170. if (!lock_task_sighand(p, &flags))
  1171. return;
  1172. switch (who) {
  1173. case RUSAGE_BOTH:
  1174. case RUSAGE_CHILDREN:
  1175. utime = p->signal->cutime;
  1176. stime = p->signal->cstime;
  1177. r->ru_nvcsw = p->signal->cnvcsw;
  1178. r->ru_nivcsw = p->signal->cnivcsw;
  1179. r->ru_minflt = p->signal->cmin_flt;
  1180. r->ru_majflt = p->signal->cmaj_flt;
  1181. r->ru_inblock = p->signal->cinblock;
  1182. r->ru_oublock = p->signal->coublock;
  1183. maxrss = p->signal->cmaxrss;
  1184. if (who == RUSAGE_CHILDREN)
  1185. break;
  1186. case RUSAGE_SELF:
  1187. thread_group_times(p, &tgutime, &tgstime);
  1188. utime = cputime_add(utime, tgutime);
  1189. stime = cputime_add(stime, tgstime);
  1190. r->ru_nvcsw += p->signal->nvcsw;
  1191. r->ru_nivcsw += p->signal->nivcsw;
  1192. r->ru_minflt += p->signal->min_flt;
  1193. r->ru_majflt += p->signal->maj_flt;
  1194. r->ru_inblock += p->signal->inblock;
  1195. r->ru_oublock += p->signal->oublock;
  1196. if (maxrss < p->signal->maxrss)
  1197. maxrss = p->signal->maxrss;
  1198. t = p;
  1199. do {
  1200. accumulate_thread_rusage(t, r);
  1201. t = next_thread(t);
  1202. } while (t != p);
  1203. break;
  1204. default:
  1205. BUG();
  1206. }
  1207. unlock_task_sighand(p, &flags);
  1208. out:
  1209. cputime_to_timeval(utime, &r->ru_utime);
  1210. cputime_to_timeval(stime, &r->ru_stime);
  1211. if (who != RUSAGE_CHILDREN) {
  1212. struct mm_struct *mm = get_task_mm(p);
  1213. if (mm) {
  1214. setmax_mm_hiwater_rss(&maxrss, mm);
  1215. mmput(mm);
  1216. }
  1217. }
  1218. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1219. }
  1220. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1221. {
  1222. struct rusage r;
  1223. k_getrusage(p, who, &r);
  1224. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1225. }
  1226. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1227. {
  1228. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1229. who != RUSAGE_THREAD)
  1230. return -EINVAL;
  1231. return getrusage(current, who, ru);
  1232. }
  1233. SYSCALL_DEFINE1(umask, int, mask)
  1234. {
  1235. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1236. return mask;
  1237. }
  1238. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1239. unsigned long, arg4, unsigned long, arg5)
  1240. {
  1241. struct task_struct *me = current;
  1242. unsigned char comm[sizeof(me->comm)];
  1243. long error;
  1244. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1245. if (error != -ENOSYS)
  1246. return error;
  1247. error = 0;
  1248. switch (option) {
  1249. case PR_SET_PDEATHSIG:
  1250. if (!valid_signal(arg2)) {
  1251. error = -EINVAL;
  1252. break;
  1253. }
  1254. me->pdeath_signal = arg2;
  1255. error = 0;
  1256. break;
  1257. case PR_GET_PDEATHSIG:
  1258. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1259. break;
  1260. case PR_GET_DUMPABLE:
  1261. error = get_dumpable(me->mm);
  1262. break;
  1263. case PR_SET_DUMPABLE:
  1264. if (arg2 < 0 || arg2 > 1) {
  1265. error = -EINVAL;
  1266. break;
  1267. }
  1268. set_dumpable(me->mm, arg2);
  1269. error = 0;
  1270. break;
  1271. case PR_SET_UNALIGN:
  1272. error = SET_UNALIGN_CTL(me, arg2);
  1273. break;
  1274. case PR_GET_UNALIGN:
  1275. error = GET_UNALIGN_CTL(me, arg2);
  1276. break;
  1277. case PR_SET_FPEMU:
  1278. error = SET_FPEMU_CTL(me, arg2);
  1279. break;
  1280. case PR_GET_FPEMU:
  1281. error = GET_FPEMU_CTL(me, arg2);
  1282. break;
  1283. case PR_SET_FPEXC:
  1284. error = SET_FPEXC_CTL(me, arg2);
  1285. break;
  1286. case PR_GET_FPEXC:
  1287. error = GET_FPEXC_CTL(me, arg2);
  1288. break;
  1289. case PR_GET_TIMING:
  1290. error = PR_TIMING_STATISTICAL;
  1291. break;
  1292. case PR_SET_TIMING:
  1293. if (arg2 != PR_TIMING_STATISTICAL)
  1294. error = -EINVAL;
  1295. else
  1296. error = 0;
  1297. break;
  1298. case PR_SET_NAME:
  1299. comm[sizeof(me->comm)-1] = 0;
  1300. if (strncpy_from_user(comm, (char __user *)arg2,
  1301. sizeof(me->comm) - 1) < 0)
  1302. return -EFAULT;
  1303. set_task_comm(me, comm);
  1304. return 0;
  1305. case PR_GET_NAME:
  1306. get_task_comm(comm, me);
  1307. if (copy_to_user((char __user *)arg2, comm,
  1308. sizeof(comm)))
  1309. return -EFAULT;
  1310. return 0;
  1311. case PR_GET_ENDIAN:
  1312. error = GET_ENDIAN(me, arg2);
  1313. break;
  1314. case PR_SET_ENDIAN:
  1315. error = SET_ENDIAN(me, arg2);
  1316. break;
  1317. case PR_GET_SECCOMP:
  1318. error = prctl_get_seccomp();
  1319. break;
  1320. case PR_SET_SECCOMP:
  1321. error = prctl_set_seccomp(arg2);
  1322. break;
  1323. case PR_GET_TSC:
  1324. error = GET_TSC_CTL(arg2);
  1325. break;
  1326. case PR_SET_TSC:
  1327. error = SET_TSC_CTL(arg2);
  1328. break;
  1329. case PR_TASK_PERF_EVENTS_DISABLE:
  1330. error = perf_event_task_disable();
  1331. break;
  1332. case PR_TASK_PERF_EVENTS_ENABLE:
  1333. error = perf_event_task_enable();
  1334. break;
  1335. case PR_GET_TIMERSLACK:
  1336. error = current->timer_slack_ns;
  1337. break;
  1338. case PR_SET_TIMERSLACK:
  1339. if (arg2 <= 0)
  1340. current->timer_slack_ns =
  1341. current->default_timer_slack_ns;
  1342. else
  1343. current->timer_slack_ns = arg2;
  1344. error = 0;
  1345. break;
  1346. case PR_MCE_KILL:
  1347. if (arg4 | arg5)
  1348. return -EINVAL;
  1349. switch (arg2) {
  1350. case PR_MCE_KILL_CLEAR:
  1351. if (arg3 != 0)
  1352. return -EINVAL;
  1353. current->flags &= ~PF_MCE_PROCESS;
  1354. break;
  1355. case PR_MCE_KILL_SET:
  1356. current->flags |= PF_MCE_PROCESS;
  1357. if (arg3 == PR_MCE_KILL_EARLY)
  1358. current->flags |= PF_MCE_EARLY;
  1359. else if (arg3 == PR_MCE_KILL_LATE)
  1360. current->flags &= ~PF_MCE_EARLY;
  1361. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1362. current->flags &=
  1363. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1364. else
  1365. return -EINVAL;
  1366. break;
  1367. default:
  1368. return -EINVAL;
  1369. }
  1370. error = 0;
  1371. break;
  1372. case PR_MCE_KILL_GET:
  1373. if (arg2 | arg3 | arg4 | arg5)
  1374. return -EINVAL;
  1375. if (current->flags & PF_MCE_PROCESS)
  1376. error = (current->flags & PF_MCE_EARLY) ?
  1377. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1378. else
  1379. error = PR_MCE_KILL_DEFAULT;
  1380. break;
  1381. default:
  1382. error = -EINVAL;
  1383. break;
  1384. }
  1385. return error;
  1386. }
  1387. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1388. struct getcpu_cache __user *, unused)
  1389. {
  1390. int err = 0;
  1391. int cpu = raw_smp_processor_id();
  1392. if (cpup)
  1393. err |= put_user(cpu, cpup);
  1394. if (nodep)
  1395. err |= put_user(cpu_to_node(cpu), nodep);
  1396. return err ? -EFAULT : 0;
  1397. }
  1398. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1399. static void argv_cleanup(char **argv, char **envp)
  1400. {
  1401. argv_free(argv);
  1402. }
  1403. /**
  1404. * orderly_poweroff - Trigger an orderly system poweroff
  1405. * @force: force poweroff if command execution fails
  1406. *
  1407. * This may be called from any context to trigger a system shutdown.
  1408. * If the orderly shutdown fails, it will force an immediate shutdown.
  1409. */
  1410. int orderly_poweroff(bool force)
  1411. {
  1412. int argc;
  1413. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1414. static char *envp[] = {
  1415. "HOME=/",
  1416. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1417. NULL
  1418. };
  1419. int ret = -ENOMEM;
  1420. struct subprocess_info *info;
  1421. if (argv == NULL) {
  1422. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1423. __func__, poweroff_cmd);
  1424. goto out;
  1425. }
  1426. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1427. if (info == NULL) {
  1428. argv_free(argv);
  1429. goto out;
  1430. }
  1431. call_usermodehelper_setcleanup(info, argv_cleanup);
  1432. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1433. out:
  1434. if (ret && force) {
  1435. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1436. "forcing the issue\n");
  1437. /* I guess this should try to kick off some daemon to
  1438. sync and poweroff asap. Or not even bother syncing
  1439. if we're doing an emergency shutdown? */
  1440. emergency_sync();
  1441. kernel_power_off();
  1442. }
  1443. return ret;
  1444. }
  1445. EXPORT_SYMBOL_GPL(orderly_poweroff);