sys.c 42 KB

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