sys.c 43 KB

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