sys.c 41 KB

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