sys.c 45 KB

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