sys.c 43 KB

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