sys.c 44 KB

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