sys.c 42 KB

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