sys.c 43 KB

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