sys.c 42 KB

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