sys.c 40 KB

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