sys.c 42 KB

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