sys.c 41 KB

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