sys.c 37 KB

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