sys.c 43 KB

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