sys.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719
  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/personality.h>
  36. #include <linux/ptrace.h>
  37. #include <linux/fs_struct.h>
  38. #include <linux/compat.h>
  39. #include <linux/syscalls.h>
  40. #include <linux/kprobes.h>
  41. #include <linux/user_namespace.h>
  42. #include <asm/uaccess.h>
  43. #include <asm/io.h>
  44. #include <asm/unistd.h>
  45. #ifndef SET_UNALIGN_CTL
  46. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  47. #endif
  48. #ifndef GET_UNALIGN_CTL
  49. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  50. #endif
  51. #ifndef SET_FPEMU_CTL
  52. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  53. #endif
  54. #ifndef GET_FPEMU_CTL
  55. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  56. #endif
  57. #ifndef SET_FPEXC_CTL
  58. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  59. #endif
  60. #ifndef GET_FPEXC_CTL
  61. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  62. #endif
  63. #ifndef GET_ENDIAN
  64. # define GET_ENDIAN(a,b) (-EINVAL)
  65. #endif
  66. #ifndef SET_ENDIAN
  67. # define SET_ENDIAN(a,b) (-EINVAL)
  68. #endif
  69. #ifndef GET_TSC_CTL
  70. # define GET_TSC_CTL(a) (-EINVAL)
  71. #endif
  72. #ifndef SET_TSC_CTL
  73. # define SET_TSC_CTL(a) (-EINVAL)
  74. #endif
  75. /*
  76. * this is where the system-wide overflow UID and GID are defined, for
  77. * architectures that now have 32-bit UID/GID but didn't in the past
  78. */
  79. int overflowuid = DEFAULT_OVERFLOWUID;
  80. int overflowgid = DEFAULT_OVERFLOWGID;
  81. #ifdef CONFIG_UID16
  82. EXPORT_SYMBOL(overflowuid);
  83. EXPORT_SYMBOL(overflowgid);
  84. #endif
  85. /*
  86. * the same as above, but for filesystems which can only store a 16-bit
  87. * UID and GID. as such, this is needed on all architectures
  88. */
  89. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  90. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  91. EXPORT_SYMBOL(fs_overflowuid);
  92. EXPORT_SYMBOL(fs_overflowgid);
  93. /*
  94. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  95. */
  96. int C_A_D = 1;
  97. struct pid *cad_pid;
  98. EXPORT_SYMBOL(cad_pid);
  99. /*
  100. * If set, this is used for preparing the system to power off.
  101. */
  102. void (*pm_power_off_prepare)(void);
  103. /*
  104. * set the priority of a task
  105. * - the caller must hold the RCU read lock
  106. */
  107. static int set_one_prio(struct task_struct *p, int niceval, int error)
  108. {
  109. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  110. int no_nice;
  111. if (pcred->uid != cred->euid &&
  112. pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
  113. error = -EPERM;
  114. goto out;
  115. }
  116. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  117. error = -EACCES;
  118. goto out;
  119. }
  120. no_nice = security_task_setnice(p, niceval);
  121. if (no_nice) {
  122. error = no_nice;
  123. goto out;
  124. }
  125. if (error == -ESRCH)
  126. error = 0;
  127. set_user_nice(p, niceval);
  128. out:
  129. return error;
  130. }
  131. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  132. {
  133. struct task_struct *g, *p;
  134. struct user_struct *user;
  135. const struct cred *cred = current_cred();
  136. int error = -EINVAL;
  137. struct pid *pgrp;
  138. if (which > PRIO_USER || which < PRIO_PROCESS)
  139. goto out;
  140. /* normalize: avoid signed division (rounding problems) */
  141. error = -ESRCH;
  142. if (niceval < -20)
  143. niceval = -20;
  144. if (niceval > 19)
  145. niceval = 19;
  146. rcu_read_lock();
  147. read_lock(&tasklist_lock);
  148. switch (which) {
  149. case PRIO_PROCESS:
  150. if (who)
  151. p = find_task_by_vpid(who);
  152. else
  153. p = current;
  154. if (p)
  155. error = set_one_prio(p, niceval, error);
  156. break;
  157. case PRIO_PGRP:
  158. if (who)
  159. pgrp = find_vpid(who);
  160. else
  161. pgrp = task_pgrp(current);
  162. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  163. error = set_one_prio(p, niceval, error);
  164. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  165. break;
  166. case PRIO_USER:
  167. user = (struct user_struct *) cred->user;
  168. if (!who)
  169. who = cred->uid;
  170. else if ((who != cred->uid) &&
  171. !(user = find_user(who)))
  172. goto out_unlock; /* No processes for this user */
  173. do_each_thread(g, p) {
  174. if (__task_cred(p)->uid == who)
  175. error = set_one_prio(p, niceval, error);
  176. } while_each_thread(g, p);
  177. if (who != cred->uid)
  178. free_uid(user); /* For find_user() */
  179. break;
  180. }
  181. out_unlock:
  182. read_unlock(&tasklist_lock);
  183. rcu_read_unlock();
  184. out:
  185. return error;
  186. }
  187. /*
  188. * Ugh. To avoid negative return values, "getpriority()" will
  189. * not return the normal nice-value, but a negated value that
  190. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  191. * to stay compatible.
  192. */
  193. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  194. {
  195. struct task_struct *g, *p;
  196. struct user_struct *user;
  197. const struct cred *cred = current_cred();
  198. long niceval, retval = -ESRCH;
  199. struct pid *pgrp;
  200. if (which > PRIO_USER || which < PRIO_PROCESS)
  201. return -EINVAL;
  202. rcu_read_lock();
  203. read_lock(&tasklist_lock);
  204. switch (which) {
  205. case PRIO_PROCESS:
  206. if (who)
  207. p = find_task_by_vpid(who);
  208. else
  209. p = current;
  210. if (p) {
  211. niceval = 20 - task_nice(p);
  212. if (niceval > retval)
  213. retval = niceval;
  214. }
  215. break;
  216. case PRIO_PGRP:
  217. if (who)
  218. pgrp = find_vpid(who);
  219. else
  220. pgrp = task_pgrp(current);
  221. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  222. niceval = 20 - task_nice(p);
  223. if (niceval > retval)
  224. retval = niceval;
  225. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  226. break;
  227. case PRIO_USER:
  228. user = (struct user_struct *) cred->user;
  229. if (!who)
  230. who = cred->uid;
  231. else if ((who != cred->uid) &&
  232. !(user = find_user(who)))
  233. goto out_unlock; /* No processes for this user */
  234. do_each_thread(g, p) {
  235. if (__task_cred(p)->uid == who) {
  236. niceval = 20 - task_nice(p);
  237. if (niceval > retval)
  238. retval = niceval;
  239. }
  240. } while_each_thread(g, p);
  241. if (who != cred->uid)
  242. free_uid(user); /* for find_user() */
  243. break;
  244. }
  245. out_unlock:
  246. read_unlock(&tasklist_lock);
  247. rcu_read_unlock();
  248. return retval;
  249. }
  250. /**
  251. * emergency_restart - reboot the system
  252. *
  253. * Without shutting down any hardware or taking any locks
  254. * reboot the system. This is called when we know we are in
  255. * trouble so this is our best effort to reboot. This is
  256. * safe to call in interrupt context.
  257. */
  258. void emergency_restart(void)
  259. {
  260. machine_emergency_restart();
  261. }
  262. EXPORT_SYMBOL_GPL(emergency_restart);
  263. void kernel_restart_prepare(char *cmd)
  264. {
  265. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  266. system_state = SYSTEM_RESTART;
  267. device_shutdown();
  268. sysdev_shutdown();
  269. }
  270. /**
  271. * kernel_restart - reboot the system
  272. * @cmd: pointer to buffer containing command to execute for restart
  273. * or %NULL
  274. *
  275. * Shutdown everything and perform a clean reboot.
  276. * This is not safe to call in interrupt context.
  277. */
  278. void kernel_restart(char *cmd)
  279. {
  280. kernel_restart_prepare(cmd);
  281. if (!cmd)
  282. printk(KERN_EMERG "Restarting system.\n");
  283. else
  284. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  285. machine_restart(cmd);
  286. }
  287. EXPORT_SYMBOL_GPL(kernel_restart);
  288. static void kernel_shutdown_prepare(enum system_states state)
  289. {
  290. blocking_notifier_call_chain(&reboot_notifier_list,
  291. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  292. system_state = state;
  293. device_shutdown();
  294. }
  295. /**
  296. * kernel_halt - halt the system
  297. *
  298. * Shutdown everything and perform a clean system halt.
  299. */
  300. void kernel_halt(void)
  301. {
  302. kernel_shutdown_prepare(SYSTEM_HALT);
  303. sysdev_shutdown();
  304. printk(KERN_EMERG "System halted.\n");
  305. machine_halt();
  306. }
  307. EXPORT_SYMBOL_GPL(kernel_halt);
  308. /**
  309. * kernel_power_off - power_off the system
  310. *
  311. * Shutdown everything and perform a clean system power_off.
  312. */
  313. void kernel_power_off(void)
  314. {
  315. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  316. if (pm_power_off_prepare)
  317. pm_power_off_prepare();
  318. disable_nonboot_cpus();
  319. sysdev_shutdown();
  320. printk(KERN_EMERG "Power down.\n");
  321. machine_power_off();
  322. }
  323. EXPORT_SYMBOL_GPL(kernel_power_off);
  324. static DEFINE_MUTEX(reboot_mutex);
  325. /*
  326. * Reboot system call: for obvious reasons only root may call it,
  327. * and even root needs to set up some magic numbers in the registers
  328. * so that some mistake won't make this reboot the whole machine.
  329. * You can also set the meaning of the ctrl-alt-del-key here.
  330. *
  331. * reboot doesn't sync: do that yourself before calling this.
  332. */
  333. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  334. void __user *, arg)
  335. {
  336. char buffer[256];
  337. int ret = 0;
  338. /* We only trust the superuser with rebooting the system. */
  339. if (!capable(CAP_SYS_BOOT))
  340. return -EPERM;
  341. /* For safety, we require "magic" arguments. */
  342. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  343. (magic2 != LINUX_REBOOT_MAGIC2 &&
  344. magic2 != LINUX_REBOOT_MAGIC2A &&
  345. magic2 != LINUX_REBOOT_MAGIC2B &&
  346. magic2 != LINUX_REBOOT_MAGIC2C))
  347. return -EINVAL;
  348. /* Instead of trying to make the power_off code look like
  349. * halt when pm_power_off is not set do it the easy way.
  350. */
  351. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  352. cmd = LINUX_REBOOT_CMD_HALT;
  353. mutex_lock(&reboot_mutex);
  354. switch (cmd) {
  355. case LINUX_REBOOT_CMD_RESTART:
  356. kernel_restart(NULL);
  357. break;
  358. case LINUX_REBOOT_CMD_CAD_ON:
  359. C_A_D = 1;
  360. break;
  361. case LINUX_REBOOT_CMD_CAD_OFF:
  362. C_A_D = 0;
  363. break;
  364. case LINUX_REBOOT_CMD_HALT:
  365. kernel_halt();
  366. do_exit(0);
  367. panic("cannot halt");
  368. case LINUX_REBOOT_CMD_POWER_OFF:
  369. kernel_power_off();
  370. do_exit(0);
  371. break;
  372. case LINUX_REBOOT_CMD_RESTART2:
  373. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  374. ret = -EFAULT;
  375. break;
  376. }
  377. buffer[sizeof(buffer) - 1] = '\0';
  378. kernel_restart(buffer);
  379. break;
  380. #ifdef CONFIG_KEXEC
  381. case LINUX_REBOOT_CMD_KEXEC:
  382. ret = kernel_kexec();
  383. break;
  384. #endif
  385. #ifdef CONFIG_HIBERNATION
  386. case LINUX_REBOOT_CMD_SW_SUSPEND:
  387. ret = hibernate();
  388. break;
  389. #endif
  390. default:
  391. ret = -EINVAL;
  392. break;
  393. }
  394. mutex_unlock(&reboot_mutex);
  395. return ret;
  396. }
  397. static void deferred_cad(struct work_struct *dummy)
  398. {
  399. kernel_restart(NULL);
  400. }
  401. /*
  402. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  403. * As it's called within an interrupt, it may NOT sync: the only choice
  404. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  405. */
  406. void ctrl_alt_del(void)
  407. {
  408. static DECLARE_WORK(cad_work, deferred_cad);
  409. if (C_A_D)
  410. schedule_work(&cad_work);
  411. else
  412. kill_cad_pid(SIGINT, 1);
  413. }
  414. /*
  415. * Unprivileged users may change the real gid to the effective gid
  416. * or vice versa. (BSD-style)
  417. *
  418. * If you set the real gid at all, or set the effective gid to a value not
  419. * equal to the real gid, then the saved gid is set to the new effective gid.
  420. *
  421. * This makes it possible for a setgid program to completely drop its
  422. * privileges, which is often a useful assertion to make when you are doing
  423. * a security audit over a program.
  424. *
  425. * The general idea is that a program which uses just setregid() will be
  426. * 100% compatible with BSD. A program which uses just setgid() will be
  427. * 100% compatible with POSIX with saved IDs.
  428. *
  429. * SMP: There are not races, the GIDs are checked only by filesystem
  430. * operations (as far as semantic preservation is concerned).
  431. */
  432. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  433. {
  434. const struct cred *old;
  435. struct cred *new;
  436. int retval;
  437. new = prepare_creds();
  438. if (!new)
  439. return -ENOMEM;
  440. old = current_cred();
  441. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  442. if (retval)
  443. goto error;
  444. retval = -EPERM;
  445. if (rgid != (gid_t) -1) {
  446. if (old->gid == rgid ||
  447. old->egid == rgid ||
  448. capable(CAP_SETGID))
  449. new->gid = rgid;
  450. else
  451. goto error;
  452. }
  453. if (egid != (gid_t) -1) {
  454. if (old->gid == egid ||
  455. old->egid == egid ||
  456. old->sgid == egid ||
  457. capable(CAP_SETGID))
  458. new->egid = egid;
  459. else
  460. goto error;
  461. }
  462. if (rgid != (gid_t) -1 ||
  463. (egid != (gid_t) -1 && egid != old->gid))
  464. new->sgid = new->egid;
  465. new->fsgid = new->egid;
  466. return commit_creds(new);
  467. error:
  468. abort_creds(new);
  469. return retval;
  470. }
  471. /*
  472. * setgid() is implemented like SysV w/ SAVED_IDS
  473. *
  474. * SMP: Same implicit races as above.
  475. */
  476. SYSCALL_DEFINE1(setgid, gid_t, gid)
  477. {
  478. const struct cred *old;
  479. struct cred *new;
  480. int retval;
  481. new = prepare_creds();
  482. if (!new)
  483. return -ENOMEM;
  484. old = current_cred();
  485. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  486. if (retval)
  487. goto error;
  488. retval = -EPERM;
  489. if (capable(CAP_SETGID))
  490. new->gid = new->egid = new->sgid = new->fsgid = gid;
  491. else if (gid == old->gid || gid == old->sgid)
  492. new->egid = new->fsgid = gid;
  493. else
  494. goto error;
  495. return commit_creds(new);
  496. error:
  497. abort_creds(new);
  498. return retval;
  499. }
  500. /*
  501. * change the user struct in a credentials set to match the new UID
  502. */
  503. static int set_user(struct cred *new)
  504. {
  505. struct user_struct *new_user;
  506. new_user = alloc_uid(current_user_ns(), new->uid);
  507. if (!new_user)
  508. return -EAGAIN;
  509. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  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. #ifdef COMPAT_UTS_MACHINE
  967. #define override_architecture(name) \
  968. (current->personality == PER_LINUX32 && \
  969. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  970. sizeof(COMPAT_UTS_MACHINE)))
  971. #else
  972. #define override_architecture(name) 0
  973. #endif
  974. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  975. {
  976. int errno = 0;
  977. down_read(&uts_sem);
  978. if (copy_to_user(name, utsname(), sizeof *name))
  979. errno = -EFAULT;
  980. up_read(&uts_sem);
  981. if (!errno && override_architecture(name))
  982. errno = -EFAULT;
  983. return errno;
  984. }
  985. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  986. /*
  987. * Old cruft
  988. */
  989. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  990. {
  991. int error = 0;
  992. if (!name)
  993. return -EFAULT;
  994. down_read(&uts_sem);
  995. if (copy_to_user(name, utsname(), sizeof(*name)))
  996. error = -EFAULT;
  997. up_read(&uts_sem);
  998. if (!error && override_architecture(name))
  999. error = -EFAULT;
  1000. return error;
  1001. }
  1002. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1003. {
  1004. int error;
  1005. if (!name)
  1006. return -EFAULT;
  1007. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1008. return -EFAULT;
  1009. down_read(&uts_sem);
  1010. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1011. __OLD_UTS_LEN);
  1012. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1013. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1014. __OLD_UTS_LEN);
  1015. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1016. error |= __copy_to_user(&name->release, &utsname()->release,
  1017. __OLD_UTS_LEN);
  1018. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1019. error |= __copy_to_user(&name->version, &utsname()->version,
  1020. __OLD_UTS_LEN);
  1021. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1022. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1023. __OLD_UTS_LEN);
  1024. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1025. up_read(&uts_sem);
  1026. if (!error && override_architecture(name))
  1027. error = -EFAULT;
  1028. return error ? -EFAULT : 0;
  1029. }
  1030. #endif
  1031. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1032. {
  1033. int errno;
  1034. char tmp[__NEW_UTS_LEN];
  1035. if (!capable(CAP_SYS_ADMIN))
  1036. return -EPERM;
  1037. if (len < 0 || len > __NEW_UTS_LEN)
  1038. return -EINVAL;
  1039. down_write(&uts_sem);
  1040. errno = -EFAULT;
  1041. if (!copy_from_user(tmp, name, len)) {
  1042. struct new_utsname *u = utsname();
  1043. memcpy(u->nodename, tmp, len);
  1044. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1045. errno = 0;
  1046. }
  1047. up_write(&uts_sem);
  1048. return errno;
  1049. }
  1050. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1051. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1052. {
  1053. int i, errno;
  1054. struct new_utsname *u;
  1055. if (len < 0)
  1056. return -EINVAL;
  1057. down_read(&uts_sem);
  1058. u = utsname();
  1059. i = 1 + strlen(u->nodename);
  1060. if (i > len)
  1061. i = len;
  1062. errno = 0;
  1063. if (copy_to_user(name, u->nodename, i))
  1064. errno = -EFAULT;
  1065. up_read(&uts_sem);
  1066. return errno;
  1067. }
  1068. #endif
  1069. /*
  1070. * Only setdomainname; getdomainname can be implemented by calling
  1071. * uname()
  1072. */
  1073. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1074. {
  1075. int errno;
  1076. char tmp[__NEW_UTS_LEN];
  1077. if (!capable(CAP_SYS_ADMIN))
  1078. return -EPERM;
  1079. if (len < 0 || len > __NEW_UTS_LEN)
  1080. return -EINVAL;
  1081. down_write(&uts_sem);
  1082. errno = -EFAULT;
  1083. if (!copy_from_user(tmp, name, len)) {
  1084. struct new_utsname *u = utsname();
  1085. memcpy(u->domainname, tmp, len);
  1086. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1087. errno = 0;
  1088. }
  1089. up_write(&uts_sem);
  1090. return errno;
  1091. }
  1092. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1093. {
  1094. if (resource >= RLIM_NLIMITS)
  1095. return -EINVAL;
  1096. else {
  1097. struct rlimit value;
  1098. task_lock(current->group_leader);
  1099. value = current->signal->rlim[resource];
  1100. task_unlock(current->group_leader);
  1101. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1102. }
  1103. }
  1104. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1105. /*
  1106. * Back compatibility for getrlimit. Needed for some apps.
  1107. */
  1108. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1109. struct rlimit __user *, rlim)
  1110. {
  1111. struct rlimit x;
  1112. if (resource >= RLIM_NLIMITS)
  1113. return -EINVAL;
  1114. task_lock(current->group_leader);
  1115. x = current->signal->rlim[resource];
  1116. task_unlock(current->group_leader);
  1117. if (x.rlim_cur > 0x7FFFFFFF)
  1118. x.rlim_cur = 0x7FFFFFFF;
  1119. if (x.rlim_max > 0x7FFFFFFF)
  1120. x.rlim_max = 0x7FFFFFFF;
  1121. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1122. }
  1123. #endif
  1124. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1125. {
  1126. struct rlimit new_rlim, *old_rlim;
  1127. int retval;
  1128. if (resource >= RLIM_NLIMITS)
  1129. return -EINVAL;
  1130. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1131. return -EFAULT;
  1132. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1133. return -EINVAL;
  1134. old_rlim = current->signal->rlim + resource;
  1135. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1136. !capable(CAP_SYS_RESOURCE))
  1137. return -EPERM;
  1138. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1139. return -EPERM;
  1140. retval = security_task_setrlimit(resource, &new_rlim);
  1141. if (retval)
  1142. return retval;
  1143. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1144. /*
  1145. * The caller is asking for an immediate RLIMIT_CPU
  1146. * expiry. But we use the zero value to mean "it was
  1147. * never set". So let's cheat and make it one second
  1148. * instead
  1149. */
  1150. new_rlim.rlim_cur = 1;
  1151. }
  1152. task_lock(current->group_leader);
  1153. *old_rlim = new_rlim;
  1154. task_unlock(current->group_leader);
  1155. if (resource != RLIMIT_CPU)
  1156. goto out;
  1157. /*
  1158. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1159. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1160. * very long-standing error, and fixing it now risks breakage of
  1161. * applications, so we live with it
  1162. */
  1163. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1164. goto out;
  1165. update_rlimit_cpu(new_rlim.rlim_cur);
  1166. out:
  1167. return 0;
  1168. }
  1169. /*
  1170. * It would make sense to put struct rusage in the task_struct,
  1171. * except that would make the task_struct be *really big*. After
  1172. * task_struct gets moved into malloc'ed memory, it would
  1173. * make sense to do this. It will make moving the rest of the information
  1174. * a lot simpler! (Which we're not doing right now because we're not
  1175. * measuring them yet).
  1176. *
  1177. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1178. * races with threads incrementing their own counters. But since word
  1179. * reads are atomic, we either get new values or old values and we don't
  1180. * care which for the sums. We always take the siglock to protect reading
  1181. * the c* fields from p->signal from races with exit.c updating those
  1182. * fields when reaping, so a sample either gets all the additions of a
  1183. * given child after it's reaped, or none so this sample is before reaping.
  1184. *
  1185. * Locking:
  1186. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1187. * for the cases current multithreaded, non-current single threaded
  1188. * non-current multithreaded. Thread traversal is now safe with
  1189. * the siglock held.
  1190. * Strictly speaking, we donot need to take the siglock if we are current and
  1191. * single threaded, as no one else can take our signal_struct away, no one
  1192. * else can reap the children to update signal->c* counters, and no one else
  1193. * can race with the signal-> fields. If we do not take any lock, the
  1194. * signal-> fields could be read out of order while another thread was just
  1195. * exiting. So we should place a read memory barrier when we avoid the lock.
  1196. * On the writer side, write memory barrier is implied in __exit_signal
  1197. * as __exit_signal releases the siglock spinlock after updating the signal->
  1198. * fields. But we don't do this yet to keep things simple.
  1199. *
  1200. */
  1201. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1202. {
  1203. r->ru_nvcsw += t->nvcsw;
  1204. r->ru_nivcsw += t->nivcsw;
  1205. r->ru_minflt += t->min_flt;
  1206. r->ru_majflt += t->maj_flt;
  1207. r->ru_inblock += task_io_get_inblock(t);
  1208. r->ru_oublock += task_io_get_oublock(t);
  1209. }
  1210. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1211. {
  1212. struct task_struct *t;
  1213. unsigned long flags;
  1214. cputime_t tgutime, tgstime, utime, stime;
  1215. unsigned long maxrss = 0;
  1216. memset((char *) r, 0, sizeof *r);
  1217. utime = stime = cputime_zero;
  1218. if (who == RUSAGE_THREAD) {
  1219. task_times(current, &utime, &stime);
  1220. accumulate_thread_rusage(p, r);
  1221. maxrss = p->signal->maxrss;
  1222. goto out;
  1223. }
  1224. if (!lock_task_sighand(p, &flags))
  1225. return;
  1226. switch (who) {
  1227. case RUSAGE_BOTH:
  1228. case RUSAGE_CHILDREN:
  1229. utime = p->signal->cutime;
  1230. stime = p->signal->cstime;
  1231. r->ru_nvcsw = p->signal->cnvcsw;
  1232. r->ru_nivcsw = p->signal->cnivcsw;
  1233. r->ru_minflt = p->signal->cmin_flt;
  1234. r->ru_majflt = p->signal->cmaj_flt;
  1235. r->ru_inblock = p->signal->cinblock;
  1236. r->ru_oublock = p->signal->coublock;
  1237. maxrss = p->signal->cmaxrss;
  1238. if (who == RUSAGE_CHILDREN)
  1239. break;
  1240. case RUSAGE_SELF:
  1241. thread_group_times(p, &tgutime, &tgstime);
  1242. utime = cputime_add(utime, tgutime);
  1243. stime = cputime_add(stime, tgstime);
  1244. r->ru_nvcsw += p->signal->nvcsw;
  1245. r->ru_nivcsw += p->signal->nivcsw;
  1246. r->ru_minflt += p->signal->min_flt;
  1247. r->ru_majflt += p->signal->maj_flt;
  1248. r->ru_inblock += p->signal->inblock;
  1249. r->ru_oublock += p->signal->oublock;
  1250. if (maxrss < p->signal->maxrss)
  1251. maxrss = p->signal->maxrss;
  1252. t = p;
  1253. do {
  1254. accumulate_thread_rusage(t, r);
  1255. t = next_thread(t);
  1256. } while (t != p);
  1257. break;
  1258. default:
  1259. BUG();
  1260. }
  1261. unlock_task_sighand(p, &flags);
  1262. out:
  1263. cputime_to_timeval(utime, &r->ru_utime);
  1264. cputime_to_timeval(stime, &r->ru_stime);
  1265. if (who != RUSAGE_CHILDREN) {
  1266. struct mm_struct *mm = get_task_mm(p);
  1267. if (mm) {
  1268. setmax_mm_hiwater_rss(&maxrss, mm);
  1269. mmput(mm);
  1270. }
  1271. }
  1272. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1273. }
  1274. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1275. {
  1276. struct rusage r;
  1277. k_getrusage(p, who, &r);
  1278. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1279. }
  1280. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1281. {
  1282. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1283. who != RUSAGE_THREAD)
  1284. return -EINVAL;
  1285. return getrusage(current, who, ru);
  1286. }
  1287. SYSCALL_DEFINE1(umask, int, mask)
  1288. {
  1289. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1290. return mask;
  1291. }
  1292. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1293. unsigned long, arg4, unsigned long, arg5)
  1294. {
  1295. struct task_struct *me = current;
  1296. unsigned char comm[sizeof(me->comm)];
  1297. long error;
  1298. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1299. if (error != -ENOSYS)
  1300. return error;
  1301. error = 0;
  1302. switch (option) {
  1303. case PR_SET_PDEATHSIG:
  1304. if (!valid_signal(arg2)) {
  1305. error = -EINVAL;
  1306. break;
  1307. }
  1308. me->pdeath_signal = arg2;
  1309. error = 0;
  1310. break;
  1311. case PR_GET_PDEATHSIG:
  1312. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1313. break;
  1314. case PR_GET_DUMPABLE:
  1315. error = get_dumpable(me->mm);
  1316. break;
  1317. case PR_SET_DUMPABLE:
  1318. if (arg2 < 0 || arg2 > 1) {
  1319. error = -EINVAL;
  1320. break;
  1321. }
  1322. set_dumpable(me->mm, arg2);
  1323. error = 0;
  1324. break;
  1325. case PR_SET_UNALIGN:
  1326. error = SET_UNALIGN_CTL(me, arg2);
  1327. break;
  1328. case PR_GET_UNALIGN:
  1329. error = GET_UNALIGN_CTL(me, arg2);
  1330. break;
  1331. case PR_SET_FPEMU:
  1332. error = SET_FPEMU_CTL(me, arg2);
  1333. break;
  1334. case PR_GET_FPEMU:
  1335. error = GET_FPEMU_CTL(me, arg2);
  1336. break;
  1337. case PR_SET_FPEXC:
  1338. error = SET_FPEXC_CTL(me, arg2);
  1339. break;
  1340. case PR_GET_FPEXC:
  1341. error = GET_FPEXC_CTL(me, arg2);
  1342. break;
  1343. case PR_GET_TIMING:
  1344. error = PR_TIMING_STATISTICAL;
  1345. break;
  1346. case PR_SET_TIMING:
  1347. if (arg2 != PR_TIMING_STATISTICAL)
  1348. error = -EINVAL;
  1349. else
  1350. error = 0;
  1351. break;
  1352. case PR_SET_NAME:
  1353. comm[sizeof(me->comm)-1] = 0;
  1354. if (strncpy_from_user(comm, (char __user *)arg2,
  1355. sizeof(me->comm) - 1) < 0)
  1356. return -EFAULT;
  1357. set_task_comm(me, comm);
  1358. return 0;
  1359. case PR_GET_NAME:
  1360. get_task_comm(comm, me);
  1361. if (copy_to_user((char __user *)arg2, comm,
  1362. sizeof(comm)))
  1363. return -EFAULT;
  1364. return 0;
  1365. case PR_GET_ENDIAN:
  1366. error = GET_ENDIAN(me, arg2);
  1367. break;
  1368. case PR_SET_ENDIAN:
  1369. error = SET_ENDIAN(me, arg2);
  1370. break;
  1371. case PR_GET_SECCOMP:
  1372. error = prctl_get_seccomp();
  1373. break;
  1374. case PR_SET_SECCOMP:
  1375. error = prctl_set_seccomp(arg2);
  1376. break;
  1377. case PR_GET_TSC:
  1378. error = GET_TSC_CTL(arg2);
  1379. break;
  1380. case PR_SET_TSC:
  1381. error = SET_TSC_CTL(arg2);
  1382. break;
  1383. case PR_TASK_PERF_EVENTS_DISABLE:
  1384. error = perf_event_task_disable();
  1385. break;
  1386. case PR_TASK_PERF_EVENTS_ENABLE:
  1387. error = perf_event_task_enable();
  1388. break;
  1389. case PR_GET_TIMERSLACK:
  1390. error = current->timer_slack_ns;
  1391. break;
  1392. case PR_SET_TIMERSLACK:
  1393. if (arg2 <= 0)
  1394. current->timer_slack_ns =
  1395. current->default_timer_slack_ns;
  1396. else
  1397. current->timer_slack_ns = arg2;
  1398. error = 0;
  1399. break;
  1400. case PR_MCE_KILL:
  1401. if (arg4 | arg5)
  1402. return -EINVAL;
  1403. switch (arg2) {
  1404. case PR_MCE_KILL_CLEAR:
  1405. if (arg3 != 0)
  1406. return -EINVAL;
  1407. current->flags &= ~PF_MCE_PROCESS;
  1408. break;
  1409. case PR_MCE_KILL_SET:
  1410. current->flags |= PF_MCE_PROCESS;
  1411. if (arg3 == PR_MCE_KILL_EARLY)
  1412. current->flags |= PF_MCE_EARLY;
  1413. else if (arg3 == PR_MCE_KILL_LATE)
  1414. current->flags &= ~PF_MCE_EARLY;
  1415. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1416. current->flags &=
  1417. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1418. else
  1419. return -EINVAL;
  1420. break;
  1421. default:
  1422. return -EINVAL;
  1423. }
  1424. error = 0;
  1425. break;
  1426. case PR_MCE_KILL_GET:
  1427. if (arg2 | arg3 | arg4 | arg5)
  1428. return -EINVAL;
  1429. if (current->flags & PF_MCE_PROCESS)
  1430. error = (current->flags & PF_MCE_EARLY) ?
  1431. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1432. else
  1433. error = PR_MCE_KILL_DEFAULT;
  1434. break;
  1435. default:
  1436. error = -EINVAL;
  1437. break;
  1438. }
  1439. return error;
  1440. }
  1441. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1442. struct getcpu_cache __user *, unused)
  1443. {
  1444. int err = 0;
  1445. int cpu = raw_smp_processor_id();
  1446. if (cpup)
  1447. err |= put_user(cpu, cpup);
  1448. if (nodep)
  1449. err |= put_user(cpu_to_node(cpu), nodep);
  1450. return err ? -EFAULT : 0;
  1451. }
  1452. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1453. static void argv_cleanup(char **argv, char **envp)
  1454. {
  1455. argv_free(argv);
  1456. }
  1457. /**
  1458. * orderly_poweroff - Trigger an orderly system poweroff
  1459. * @force: force poweroff if command execution fails
  1460. *
  1461. * This may be called from any context to trigger a system shutdown.
  1462. * If the orderly shutdown fails, it will force an immediate shutdown.
  1463. */
  1464. int orderly_poweroff(bool force)
  1465. {
  1466. int argc;
  1467. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1468. static char *envp[] = {
  1469. "HOME=/",
  1470. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1471. NULL
  1472. };
  1473. int ret = -ENOMEM;
  1474. struct subprocess_info *info;
  1475. if (argv == NULL) {
  1476. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1477. __func__, poweroff_cmd);
  1478. goto out;
  1479. }
  1480. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1481. if (info == NULL) {
  1482. argv_free(argv);
  1483. goto out;
  1484. }
  1485. call_usermodehelper_setcleanup(info, argv_cleanup);
  1486. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1487. out:
  1488. if (ret && force) {
  1489. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1490. "forcing the issue\n");
  1491. /* I guess this should try to kick off some daemon to
  1492. sync and poweroff asap. Or not even bother syncing
  1493. if we're doing an emergency shutdown? */
  1494. emergency_sync();
  1495. kernel_power_off();
  1496. }
  1497. return ret;
  1498. }
  1499. EXPORT_SYMBOL_GPL(orderly_poweroff);