sys.c 43 KB

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