sys.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704
  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 = -EPERM;
  543. if (ruid != (uid_t) -1) {
  544. new->uid = ruid;
  545. if (old->uid != ruid &&
  546. old->euid != ruid &&
  547. !capable(CAP_SETUID))
  548. goto error;
  549. }
  550. if (euid != (uid_t) -1) {
  551. new->euid = euid;
  552. if (old->uid != euid &&
  553. old->euid != euid &&
  554. old->suid != euid &&
  555. !capable(CAP_SETUID))
  556. goto error;
  557. }
  558. if (new->uid != old->uid) {
  559. retval = set_user(new);
  560. if (retval < 0)
  561. goto error;
  562. }
  563. if (ruid != (uid_t) -1 ||
  564. (euid != (uid_t) -1 && euid != old->uid))
  565. new->suid = new->euid;
  566. new->fsuid = new->euid;
  567. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  568. if (retval < 0)
  569. goto error;
  570. return commit_creds(new);
  571. error:
  572. abort_creds(new);
  573. return retval;
  574. }
  575. /*
  576. * setuid() is implemented like SysV with SAVED_IDS
  577. *
  578. * Note that SAVED_ID's is deficient in that a setuid root program
  579. * like sendmail, for example, cannot set its uid to be a normal
  580. * user and then switch back, because if you're root, setuid() sets
  581. * the saved uid too. If you don't like this, blame the bright people
  582. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  583. * will allow a root program to temporarily drop privileges and be able to
  584. * regain them by swapping the real and effective uid.
  585. */
  586. SYSCALL_DEFINE1(setuid, uid_t, uid)
  587. {
  588. const struct cred *old;
  589. struct cred *new;
  590. int retval;
  591. new = prepare_creds();
  592. if (!new)
  593. return -ENOMEM;
  594. old = current_cred();
  595. retval = -EPERM;
  596. if (capable(CAP_SETUID)) {
  597. new->suid = new->uid = uid;
  598. if (uid != old->uid) {
  599. retval = set_user(new);
  600. if (retval < 0)
  601. goto error;
  602. }
  603. } else if (uid != old->uid && uid != new->suid) {
  604. goto error;
  605. }
  606. new->fsuid = new->euid = uid;
  607. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  608. if (retval < 0)
  609. goto error;
  610. return commit_creds(new);
  611. error:
  612. abort_creds(new);
  613. return retval;
  614. }
  615. /*
  616. * This function implements a generic ability to update ruid, euid,
  617. * and suid. This allows you to implement the 4.4 compatible seteuid().
  618. */
  619. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  620. {
  621. const struct cred *old;
  622. struct cred *new;
  623. int retval;
  624. new = prepare_creds();
  625. if (!new)
  626. return -ENOMEM;
  627. old = current_cred();
  628. retval = -EPERM;
  629. if (!capable(CAP_SETUID)) {
  630. if (ruid != (uid_t) -1 && ruid != old->uid &&
  631. ruid != old->euid && ruid != old->suid)
  632. goto error;
  633. if (euid != (uid_t) -1 && euid != old->uid &&
  634. euid != old->euid && euid != old->suid)
  635. goto error;
  636. if (suid != (uid_t) -1 && suid != old->uid &&
  637. suid != old->euid && suid != old->suid)
  638. goto error;
  639. }
  640. if (ruid != (uid_t) -1) {
  641. new->uid = ruid;
  642. if (ruid != old->uid) {
  643. retval = set_user(new);
  644. if (retval < 0)
  645. goto error;
  646. }
  647. }
  648. if (euid != (uid_t) -1)
  649. new->euid = euid;
  650. if (suid != (uid_t) -1)
  651. new->suid = suid;
  652. new->fsuid = new->euid;
  653. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  654. if (retval < 0)
  655. goto error;
  656. return commit_creds(new);
  657. error:
  658. abort_creds(new);
  659. return retval;
  660. }
  661. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  662. {
  663. const struct cred *cred = current_cred();
  664. int retval;
  665. if (!(retval = put_user(cred->uid, ruid)) &&
  666. !(retval = put_user(cred->euid, euid)))
  667. retval = put_user(cred->suid, suid);
  668. return retval;
  669. }
  670. /*
  671. * Same as above, but for rgid, egid, sgid.
  672. */
  673. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  674. {
  675. const struct cred *old;
  676. struct cred *new;
  677. int retval;
  678. new = prepare_creds();
  679. if (!new)
  680. return -ENOMEM;
  681. old = current_cred();
  682. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  683. if (retval)
  684. goto error;
  685. retval = -EPERM;
  686. if (!capable(CAP_SETGID)) {
  687. if (rgid != (gid_t) -1 && rgid != old->gid &&
  688. rgid != old->egid && rgid != old->sgid)
  689. goto error;
  690. if (egid != (gid_t) -1 && egid != old->gid &&
  691. egid != old->egid && egid != old->sgid)
  692. goto error;
  693. if (sgid != (gid_t) -1 && sgid != old->gid &&
  694. sgid != old->egid && sgid != old->sgid)
  695. goto error;
  696. }
  697. if (rgid != (gid_t) -1)
  698. new->gid = rgid;
  699. if (egid != (gid_t) -1)
  700. new->egid = egid;
  701. if (sgid != (gid_t) -1)
  702. new->sgid = sgid;
  703. new->fsgid = new->egid;
  704. return commit_creds(new);
  705. error:
  706. abort_creds(new);
  707. return retval;
  708. }
  709. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  710. {
  711. const struct cred *cred = current_cred();
  712. int retval;
  713. if (!(retval = put_user(cred->gid, rgid)) &&
  714. !(retval = put_user(cred->egid, egid)))
  715. retval = put_user(cred->sgid, sgid);
  716. return retval;
  717. }
  718. /*
  719. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  720. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  721. * whatever uid it wants to). It normally shadows "euid", except when
  722. * explicitly set by setfsuid() or for access..
  723. */
  724. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  725. {
  726. const struct cred *old;
  727. struct cred *new;
  728. uid_t old_fsuid;
  729. new = prepare_creds();
  730. if (!new)
  731. return current_fsuid();
  732. old = current_cred();
  733. old_fsuid = old->fsuid;
  734. if (uid == old->uid || uid == old->euid ||
  735. uid == old->suid || uid == old->fsuid ||
  736. capable(CAP_SETUID)) {
  737. if (uid != old_fsuid) {
  738. new->fsuid = uid;
  739. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  740. goto change_okay;
  741. }
  742. }
  743. abort_creds(new);
  744. return old_fsuid;
  745. change_okay:
  746. commit_creds(new);
  747. return old_fsuid;
  748. }
  749. /*
  750. * Samma på svenska..
  751. */
  752. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  753. {
  754. const struct cred *old;
  755. struct cred *new;
  756. gid_t old_fsgid;
  757. new = prepare_creds();
  758. if (!new)
  759. return current_fsgid();
  760. old = current_cred();
  761. old_fsgid = old->fsgid;
  762. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  763. goto error;
  764. if (gid == old->gid || gid == old->egid ||
  765. gid == old->sgid || gid == old->fsgid ||
  766. capable(CAP_SETGID)) {
  767. if (gid != old_fsgid) {
  768. new->fsgid = gid;
  769. goto change_okay;
  770. }
  771. }
  772. error:
  773. abort_creds(new);
  774. return old_fsgid;
  775. change_okay:
  776. commit_creds(new);
  777. return old_fsgid;
  778. }
  779. void do_sys_times(struct tms *tms)
  780. {
  781. cputime_t tgutime, tgstime, cutime, cstime;
  782. spin_lock_irq(&current->sighand->siglock);
  783. thread_group_times(current, &tgutime, &tgstime);
  784. cutime = current->signal->cutime;
  785. cstime = current->signal->cstime;
  786. spin_unlock_irq(&current->sighand->siglock);
  787. tms->tms_utime = cputime_to_clock_t(tgutime);
  788. tms->tms_stime = cputime_to_clock_t(tgstime);
  789. tms->tms_cutime = cputime_to_clock_t(cutime);
  790. tms->tms_cstime = cputime_to_clock_t(cstime);
  791. }
  792. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  793. {
  794. if (tbuf) {
  795. struct tms tmp;
  796. do_sys_times(&tmp);
  797. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  798. return -EFAULT;
  799. }
  800. force_successful_syscall_return();
  801. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  802. }
  803. /*
  804. * This needs some heavy checking ...
  805. * I just haven't the stomach for it. I also don't fully
  806. * understand sessions/pgrp etc. Let somebody who does explain it.
  807. *
  808. * OK, I think I have the protection semantics right.... this is really
  809. * only important on a multi-user system anyway, to make sure one user
  810. * can't send a signal to a process owned by another. -TYT, 12/12/91
  811. *
  812. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  813. * LBT 04.03.94
  814. */
  815. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  816. {
  817. struct task_struct *p;
  818. struct task_struct *group_leader = current->group_leader;
  819. struct pid *pgrp;
  820. int err;
  821. if (!pid)
  822. pid = task_pid_vnr(group_leader);
  823. if (!pgid)
  824. pgid = pid;
  825. if (pgid < 0)
  826. return -EINVAL;
  827. /* From this point forward we keep holding onto the tasklist lock
  828. * so that our parent does not change from under us. -DaveM
  829. */
  830. write_lock_irq(&tasklist_lock);
  831. err = -ESRCH;
  832. p = find_task_by_vpid(pid);
  833. if (!p)
  834. goto out;
  835. err = -EINVAL;
  836. if (!thread_group_leader(p))
  837. goto out;
  838. if (same_thread_group(p->real_parent, group_leader)) {
  839. err = -EPERM;
  840. if (task_session(p) != task_session(group_leader))
  841. goto out;
  842. err = -EACCES;
  843. if (p->did_exec)
  844. goto out;
  845. } else {
  846. err = -ESRCH;
  847. if (p != group_leader)
  848. goto out;
  849. }
  850. err = -EPERM;
  851. if (p->signal->leader)
  852. goto out;
  853. pgrp = task_pid(p);
  854. if (pgid != pid) {
  855. struct task_struct *g;
  856. pgrp = find_vpid(pgid);
  857. g = pid_task(pgrp, PIDTYPE_PGID);
  858. if (!g || task_session(g) != task_session(group_leader))
  859. goto out;
  860. }
  861. err = security_task_setpgid(p, pgid);
  862. if (err)
  863. goto out;
  864. if (task_pgrp(p) != pgrp)
  865. change_pid(p, PIDTYPE_PGID, pgrp);
  866. err = 0;
  867. out:
  868. /* All paths lead to here, thus we are safe. -DaveM */
  869. write_unlock_irq(&tasklist_lock);
  870. return err;
  871. }
  872. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  873. {
  874. struct task_struct *p;
  875. struct pid *grp;
  876. int retval;
  877. rcu_read_lock();
  878. if (!pid)
  879. grp = task_pgrp(current);
  880. else {
  881. retval = -ESRCH;
  882. p = find_task_by_vpid(pid);
  883. if (!p)
  884. goto out;
  885. grp = task_pgrp(p);
  886. if (!grp)
  887. goto out;
  888. retval = security_task_getpgid(p);
  889. if (retval)
  890. goto out;
  891. }
  892. retval = pid_vnr(grp);
  893. out:
  894. rcu_read_unlock();
  895. return retval;
  896. }
  897. #ifdef __ARCH_WANT_SYS_GETPGRP
  898. SYSCALL_DEFINE0(getpgrp)
  899. {
  900. return sys_getpgid(0);
  901. }
  902. #endif
  903. SYSCALL_DEFINE1(getsid, pid_t, pid)
  904. {
  905. struct task_struct *p;
  906. struct pid *sid;
  907. int retval;
  908. rcu_read_lock();
  909. if (!pid)
  910. sid = task_session(current);
  911. else {
  912. retval = -ESRCH;
  913. p = find_task_by_vpid(pid);
  914. if (!p)
  915. goto out;
  916. sid = task_session(p);
  917. if (!sid)
  918. goto out;
  919. retval = security_task_getsid(p);
  920. if (retval)
  921. goto out;
  922. }
  923. retval = pid_vnr(sid);
  924. out:
  925. rcu_read_unlock();
  926. return retval;
  927. }
  928. SYSCALL_DEFINE0(setsid)
  929. {
  930. struct task_struct *group_leader = current->group_leader;
  931. struct pid *sid = task_pid(group_leader);
  932. pid_t session = pid_vnr(sid);
  933. int err = -EPERM;
  934. write_lock_irq(&tasklist_lock);
  935. /* Fail if I am already a session leader */
  936. if (group_leader->signal->leader)
  937. goto out;
  938. /* Fail if a process group id already exists that equals the
  939. * proposed session id.
  940. */
  941. if (pid_task(sid, PIDTYPE_PGID))
  942. goto out;
  943. group_leader->signal->leader = 1;
  944. __set_special_pids(sid);
  945. proc_clear_tty(group_leader);
  946. err = session;
  947. out:
  948. write_unlock_irq(&tasklist_lock);
  949. if (err > 0)
  950. proc_sid_connector(group_leader);
  951. return err;
  952. }
  953. DECLARE_RWSEM(uts_sem);
  954. #ifdef COMPAT_UTS_MACHINE
  955. #define override_architecture(name) \
  956. (current->personality == PER_LINUX32 && \
  957. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  958. sizeof(COMPAT_UTS_MACHINE)))
  959. #else
  960. #define override_architecture(name) 0
  961. #endif
  962. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  963. {
  964. int errno = 0;
  965. down_read(&uts_sem);
  966. if (copy_to_user(name, utsname(), sizeof *name))
  967. errno = -EFAULT;
  968. up_read(&uts_sem);
  969. if (!errno && override_architecture(name))
  970. errno = -EFAULT;
  971. return errno;
  972. }
  973. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  974. /*
  975. * Old cruft
  976. */
  977. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  978. {
  979. int error = 0;
  980. if (!name)
  981. return -EFAULT;
  982. down_read(&uts_sem);
  983. if (copy_to_user(name, utsname(), sizeof(*name)))
  984. error = -EFAULT;
  985. up_read(&uts_sem);
  986. if (!error && override_architecture(name))
  987. error = -EFAULT;
  988. return error;
  989. }
  990. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  991. {
  992. int error;
  993. if (!name)
  994. return -EFAULT;
  995. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  996. return -EFAULT;
  997. down_read(&uts_sem);
  998. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  999. __OLD_UTS_LEN);
  1000. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1001. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1002. __OLD_UTS_LEN);
  1003. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1004. error |= __copy_to_user(&name->release, &utsname()->release,
  1005. __OLD_UTS_LEN);
  1006. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1007. error |= __copy_to_user(&name->version, &utsname()->version,
  1008. __OLD_UTS_LEN);
  1009. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1010. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1011. __OLD_UTS_LEN);
  1012. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1013. up_read(&uts_sem);
  1014. if (!error && override_architecture(name))
  1015. error = -EFAULT;
  1016. return error ? -EFAULT : 0;
  1017. }
  1018. #endif
  1019. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1020. {
  1021. int errno;
  1022. char tmp[__NEW_UTS_LEN];
  1023. if (!capable(CAP_SYS_ADMIN))
  1024. return -EPERM;
  1025. if (len < 0 || len > __NEW_UTS_LEN)
  1026. return -EINVAL;
  1027. down_write(&uts_sem);
  1028. errno = -EFAULT;
  1029. if (!copy_from_user(tmp, name, len)) {
  1030. struct new_utsname *u = utsname();
  1031. memcpy(u->nodename, tmp, len);
  1032. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1033. errno = 0;
  1034. }
  1035. up_write(&uts_sem);
  1036. return errno;
  1037. }
  1038. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1039. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1040. {
  1041. int i, errno;
  1042. struct new_utsname *u;
  1043. if (len < 0)
  1044. return -EINVAL;
  1045. down_read(&uts_sem);
  1046. u = utsname();
  1047. i = 1 + strlen(u->nodename);
  1048. if (i > len)
  1049. i = len;
  1050. errno = 0;
  1051. if (copy_to_user(name, u->nodename, i))
  1052. errno = -EFAULT;
  1053. up_read(&uts_sem);
  1054. return errno;
  1055. }
  1056. #endif
  1057. /*
  1058. * Only setdomainname; getdomainname can be implemented by calling
  1059. * uname()
  1060. */
  1061. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1062. {
  1063. int errno;
  1064. char tmp[__NEW_UTS_LEN];
  1065. if (!capable(CAP_SYS_ADMIN))
  1066. return -EPERM;
  1067. if (len < 0 || len > __NEW_UTS_LEN)
  1068. return -EINVAL;
  1069. down_write(&uts_sem);
  1070. errno = -EFAULT;
  1071. if (!copy_from_user(tmp, name, len)) {
  1072. struct new_utsname *u = utsname();
  1073. memcpy(u->domainname, tmp, len);
  1074. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1075. errno = 0;
  1076. }
  1077. up_write(&uts_sem);
  1078. return errno;
  1079. }
  1080. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1081. {
  1082. if (resource >= RLIM_NLIMITS)
  1083. return -EINVAL;
  1084. else {
  1085. struct rlimit value;
  1086. task_lock(current->group_leader);
  1087. value = current->signal->rlim[resource];
  1088. task_unlock(current->group_leader);
  1089. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1090. }
  1091. }
  1092. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1093. /*
  1094. * Back compatibility for getrlimit. Needed for some apps.
  1095. */
  1096. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1097. struct rlimit __user *, rlim)
  1098. {
  1099. struct rlimit x;
  1100. if (resource >= RLIM_NLIMITS)
  1101. return -EINVAL;
  1102. task_lock(current->group_leader);
  1103. x = current->signal->rlim[resource];
  1104. task_unlock(current->group_leader);
  1105. if (x.rlim_cur > 0x7FFFFFFF)
  1106. x.rlim_cur = 0x7FFFFFFF;
  1107. if (x.rlim_max > 0x7FFFFFFF)
  1108. x.rlim_max = 0x7FFFFFFF;
  1109. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1110. }
  1111. #endif
  1112. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1113. {
  1114. struct rlimit new_rlim, *old_rlim;
  1115. int retval;
  1116. if (resource >= RLIM_NLIMITS)
  1117. return -EINVAL;
  1118. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1119. return -EFAULT;
  1120. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1121. return -EINVAL;
  1122. old_rlim = current->signal->rlim + resource;
  1123. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1124. !capable(CAP_SYS_RESOURCE))
  1125. return -EPERM;
  1126. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1127. return -EPERM;
  1128. retval = security_task_setrlimit(resource, &new_rlim);
  1129. if (retval)
  1130. return retval;
  1131. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1132. /*
  1133. * The caller is asking for an immediate RLIMIT_CPU
  1134. * expiry. But we use the zero value to mean "it was
  1135. * never set". So let's cheat and make it one second
  1136. * instead
  1137. */
  1138. new_rlim.rlim_cur = 1;
  1139. }
  1140. task_lock(current->group_leader);
  1141. *old_rlim = new_rlim;
  1142. task_unlock(current->group_leader);
  1143. if (resource != RLIMIT_CPU)
  1144. goto out;
  1145. /*
  1146. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1147. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1148. * very long-standing error, and fixing it now risks breakage of
  1149. * applications, so we live with it
  1150. */
  1151. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1152. goto out;
  1153. update_rlimit_cpu(new_rlim.rlim_cur);
  1154. out:
  1155. return 0;
  1156. }
  1157. /*
  1158. * It would make sense to put struct rusage in the task_struct,
  1159. * except that would make the task_struct be *really big*. After
  1160. * task_struct gets moved into malloc'ed memory, it would
  1161. * make sense to do this. It will make moving the rest of the information
  1162. * a lot simpler! (Which we're not doing right now because we're not
  1163. * measuring them yet).
  1164. *
  1165. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1166. * races with threads incrementing their own counters. But since word
  1167. * reads are atomic, we either get new values or old values and we don't
  1168. * care which for the sums. We always take the siglock to protect reading
  1169. * the c* fields from p->signal from races with exit.c updating those
  1170. * fields when reaping, so a sample either gets all the additions of a
  1171. * given child after it's reaped, or none so this sample is before reaping.
  1172. *
  1173. * Locking:
  1174. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1175. * for the cases current multithreaded, non-current single threaded
  1176. * non-current multithreaded. Thread traversal is now safe with
  1177. * the siglock held.
  1178. * Strictly speaking, we donot need to take the siglock if we are current and
  1179. * single threaded, as no one else can take our signal_struct away, no one
  1180. * else can reap the children to update signal->c* counters, and no one else
  1181. * can race with the signal-> fields. If we do not take any lock, the
  1182. * signal-> fields could be read out of order while another thread was just
  1183. * exiting. So we should place a read memory barrier when we avoid the lock.
  1184. * On the writer side, write memory barrier is implied in __exit_signal
  1185. * as __exit_signal releases the siglock spinlock after updating the signal->
  1186. * fields. But we don't do this yet to keep things simple.
  1187. *
  1188. */
  1189. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1190. {
  1191. r->ru_nvcsw += t->nvcsw;
  1192. r->ru_nivcsw += t->nivcsw;
  1193. r->ru_minflt += t->min_flt;
  1194. r->ru_majflt += t->maj_flt;
  1195. r->ru_inblock += task_io_get_inblock(t);
  1196. r->ru_oublock += task_io_get_oublock(t);
  1197. }
  1198. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1199. {
  1200. struct task_struct *t;
  1201. unsigned long flags;
  1202. cputime_t tgutime, tgstime, utime, stime;
  1203. unsigned long maxrss = 0;
  1204. memset((char *) r, 0, sizeof *r);
  1205. utime = stime = cputime_zero;
  1206. if (who == RUSAGE_THREAD) {
  1207. task_times(current, &utime, &stime);
  1208. accumulate_thread_rusage(p, r);
  1209. maxrss = p->signal->maxrss;
  1210. goto out;
  1211. }
  1212. if (!lock_task_sighand(p, &flags))
  1213. return;
  1214. switch (who) {
  1215. case RUSAGE_BOTH:
  1216. case RUSAGE_CHILDREN:
  1217. utime = p->signal->cutime;
  1218. stime = p->signal->cstime;
  1219. r->ru_nvcsw = p->signal->cnvcsw;
  1220. r->ru_nivcsw = p->signal->cnivcsw;
  1221. r->ru_minflt = p->signal->cmin_flt;
  1222. r->ru_majflt = p->signal->cmaj_flt;
  1223. r->ru_inblock = p->signal->cinblock;
  1224. r->ru_oublock = p->signal->coublock;
  1225. maxrss = p->signal->cmaxrss;
  1226. if (who == RUSAGE_CHILDREN)
  1227. break;
  1228. case RUSAGE_SELF:
  1229. thread_group_times(p, &tgutime, &tgstime);
  1230. utime = cputime_add(utime, tgutime);
  1231. stime = cputime_add(stime, tgstime);
  1232. r->ru_nvcsw += p->signal->nvcsw;
  1233. r->ru_nivcsw += p->signal->nivcsw;
  1234. r->ru_minflt += p->signal->min_flt;
  1235. r->ru_majflt += p->signal->maj_flt;
  1236. r->ru_inblock += p->signal->inblock;
  1237. r->ru_oublock += p->signal->oublock;
  1238. if (maxrss < p->signal->maxrss)
  1239. maxrss = p->signal->maxrss;
  1240. t = p;
  1241. do {
  1242. accumulate_thread_rusage(t, r);
  1243. t = next_thread(t);
  1244. } while (t != p);
  1245. break;
  1246. default:
  1247. BUG();
  1248. }
  1249. unlock_task_sighand(p, &flags);
  1250. out:
  1251. cputime_to_timeval(utime, &r->ru_utime);
  1252. cputime_to_timeval(stime, &r->ru_stime);
  1253. if (who != RUSAGE_CHILDREN) {
  1254. struct mm_struct *mm = get_task_mm(p);
  1255. if (mm) {
  1256. setmax_mm_hiwater_rss(&maxrss, mm);
  1257. mmput(mm);
  1258. }
  1259. }
  1260. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1261. }
  1262. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1263. {
  1264. struct rusage r;
  1265. k_getrusage(p, who, &r);
  1266. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1267. }
  1268. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1269. {
  1270. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1271. who != RUSAGE_THREAD)
  1272. return -EINVAL;
  1273. return getrusage(current, who, ru);
  1274. }
  1275. SYSCALL_DEFINE1(umask, int, mask)
  1276. {
  1277. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1278. return mask;
  1279. }
  1280. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1281. unsigned long, arg4, unsigned long, arg5)
  1282. {
  1283. struct task_struct *me = current;
  1284. unsigned char comm[sizeof(me->comm)];
  1285. long error;
  1286. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1287. if (error != -ENOSYS)
  1288. return error;
  1289. error = 0;
  1290. switch (option) {
  1291. case PR_SET_PDEATHSIG:
  1292. if (!valid_signal(arg2)) {
  1293. error = -EINVAL;
  1294. break;
  1295. }
  1296. me->pdeath_signal = arg2;
  1297. error = 0;
  1298. break;
  1299. case PR_GET_PDEATHSIG:
  1300. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1301. break;
  1302. case PR_GET_DUMPABLE:
  1303. error = get_dumpable(me->mm);
  1304. break;
  1305. case PR_SET_DUMPABLE:
  1306. if (arg2 < 0 || arg2 > 1) {
  1307. error = -EINVAL;
  1308. break;
  1309. }
  1310. set_dumpable(me->mm, arg2);
  1311. error = 0;
  1312. break;
  1313. case PR_SET_UNALIGN:
  1314. error = SET_UNALIGN_CTL(me, arg2);
  1315. break;
  1316. case PR_GET_UNALIGN:
  1317. error = GET_UNALIGN_CTL(me, arg2);
  1318. break;
  1319. case PR_SET_FPEMU:
  1320. error = SET_FPEMU_CTL(me, arg2);
  1321. break;
  1322. case PR_GET_FPEMU:
  1323. error = GET_FPEMU_CTL(me, arg2);
  1324. break;
  1325. case PR_SET_FPEXC:
  1326. error = SET_FPEXC_CTL(me, arg2);
  1327. break;
  1328. case PR_GET_FPEXC:
  1329. error = GET_FPEXC_CTL(me, arg2);
  1330. break;
  1331. case PR_GET_TIMING:
  1332. error = PR_TIMING_STATISTICAL;
  1333. break;
  1334. case PR_SET_TIMING:
  1335. if (arg2 != PR_TIMING_STATISTICAL)
  1336. error = -EINVAL;
  1337. else
  1338. error = 0;
  1339. break;
  1340. case PR_SET_NAME:
  1341. comm[sizeof(me->comm)-1] = 0;
  1342. if (strncpy_from_user(comm, (char __user *)arg2,
  1343. sizeof(me->comm) - 1) < 0)
  1344. return -EFAULT;
  1345. set_task_comm(me, comm);
  1346. return 0;
  1347. case PR_GET_NAME:
  1348. get_task_comm(comm, me);
  1349. if (copy_to_user((char __user *)arg2, comm,
  1350. sizeof(comm)))
  1351. return -EFAULT;
  1352. return 0;
  1353. case PR_GET_ENDIAN:
  1354. error = GET_ENDIAN(me, arg2);
  1355. break;
  1356. case PR_SET_ENDIAN:
  1357. error = SET_ENDIAN(me, arg2);
  1358. break;
  1359. case PR_GET_SECCOMP:
  1360. error = prctl_get_seccomp();
  1361. break;
  1362. case PR_SET_SECCOMP:
  1363. error = prctl_set_seccomp(arg2);
  1364. break;
  1365. case PR_GET_TSC:
  1366. error = GET_TSC_CTL(arg2);
  1367. break;
  1368. case PR_SET_TSC:
  1369. error = SET_TSC_CTL(arg2);
  1370. break;
  1371. case PR_TASK_PERF_EVENTS_DISABLE:
  1372. error = perf_event_task_disable();
  1373. break;
  1374. case PR_TASK_PERF_EVENTS_ENABLE:
  1375. error = perf_event_task_enable();
  1376. break;
  1377. case PR_GET_TIMERSLACK:
  1378. error = current->timer_slack_ns;
  1379. break;
  1380. case PR_SET_TIMERSLACK:
  1381. if (arg2 <= 0)
  1382. current->timer_slack_ns =
  1383. current->default_timer_slack_ns;
  1384. else
  1385. current->timer_slack_ns = arg2;
  1386. error = 0;
  1387. break;
  1388. case PR_MCE_KILL:
  1389. if (arg4 | arg5)
  1390. return -EINVAL;
  1391. switch (arg2) {
  1392. case PR_MCE_KILL_CLEAR:
  1393. if (arg3 != 0)
  1394. return -EINVAL;
  1395. current->flags &= ~PF_MCE_PROCESS;
  1396. break;
  1397. case PR_MCE_KILL_SET:
  1398. current->flags |= PF_MCE_PROCESS;
  1399. if (arg3 == PR_MCE_KILL_EARLY)
  1400. current->flags |= PF_MCE_EARLY;
  1401. else if (arg3 == PR_MCE_KILL_LATE)
  1402. current->flags &= ~PF_MCE_EARLY;
  1403. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1404. current->flags &=
  1405. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1406. else
  1407. return -EINVAL;
  1408. break;
  1409. default:
  1410. return -EINVAL;
  1411. }
  1412. error = 0;
  1413. break;
  1414. case PR_MCE_KILL_GET:
  1415. if (arg2 | arg3 | arg4 | arg5)
  1416. return -EINVAL;
  1417. if (current->flags & PF_MCE_PROCESS)
  1418. error = (current->flags & PF_MCE_EARLY) ?
  1419. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1420. else
  1421. error = PR_MCE_KILL_DEFAULT;
  1422. break;
  1423. default:
  1424. error = -EINVAL;
  1425. break;
  1426. }
  1427. return error;
  1428. }
  1429. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1430. struct getcpu_cache __user *, unused)
  1431. {
  1432. int err = 0;
  1433. int cpu = raw_smp_processor_id();
  1434. if (cpup)
  1435. err |= put_user(cpu, cpup);
  1436. if (nodep)
  1437. err |= put_user(cpu_to_node(cpu), nodep);
  1438. return err ? -EFAULT : 0;
  1439. }
  1440. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1441. static void argv_cleanup(char **argv, char **envp)
  1442. {
  1443. argv_free(argv);
  1444. }
  1445. /**
  1446. * orderly_poweroff - Trigger an orderly system poweroff
  1447. * @force: force poweroff if command execution fails
  1448. *
  1449. * This may be called from any context to trigger a system shutdown.
  1450. * If the orderly shutdown fails, it will force an immediate shutdown.
  1451. */
  1452. int orderly_poweroff(bool force)
  1453. {
  1454. int argc;
  1455. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1456. static char *envp[] = {
  1457. "HOME=/",
  1458. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1459. NULL
  1460. };
  1461. int ret = -ENOMEM;
  1462. struct subprocess_info *info;
  1463. if (argv == NULL) {
  1464. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1465. __func__, poweroff_cmd);
  1466. goto out;
  1467. }
  1468. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1469. if (info == NULL) {
  1470. argv_free(argv);
  1471. goto out;
  1472. }
  1473. call_usermodehelper_setcleanup(info, argv_cleanup);
  1474. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1475. out:
  1476. if (ret && force) {
  1477. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1478. "forcing the issue\n");
  1479. /* I guess this should try to kick off some daemon to
  1480. sync and poweroff asap. Or not even bother syncing
  1481. if we're doing an emergency shutdown? */
  1482. emergency_sync();
  1483. kernel_power_off();
  1484. }
  1485. return ret;
  1486. }
  1487. EXPORT_SYMBOL_GPL(orderly_poweroff);