sys.c 41 KB

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