sys.c 42 KB

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