sys.c 42 KB

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