sys.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/notifier.h>
  11. #include <linux/reboot.h>
  12. #include <linux/prctl.h>
  13. #include <linux/highuid.h>
  14. #include <linux/fs.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/resource.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kexec.h>
  19. #include <linux/workqueue.h>
  20. #include <linux/capability.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/getcpu.h>
  32. #include <linux/task_io_accounting_ops.h>
  33. #include <linux/seccomp.h>
  34. #include <linux/cpu.h>
  35. #include <linux/ptrace.h>
  36. #include <linux/fs_struct.h>
  37. #include <linux/compat.h>
  38. #include <linux/syscalls.h>
  39. #include <linux/kprobes.h>
  40. #include <linux/user_namespace.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/io.h>
  43. #include <asm/unistd.h>
  44. #ifndef SET_UNALIGN_CTL
  45. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  46. #endif
  47. #ifndef GET_UNALIGN_CTL
  48. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  49. #endif
  50. #ifndef SET_FPEMU_CTL
  51. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  52. #endif
  53. #ifndef GET_FPEMU_CTL
  54. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  55. #endif
  56. #ifndef SET_FPEXC_CTL
  57. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  58. #endif
  59. #ifndef GET_FPEXC_CTL
  60. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  61. #endif
  62. #ifndef GET_ENDIAN
  63. # define GET_ENDIAN(a,b) (-EINVAL)
  64. #endif
  65. #ifndef SET_ENDIAN
  66. # define SET_ENDIAN(a,b) (-EINVAL)
  67. #endif
  68. #ifndef GET_TSC_CTL
  69. # define GET_TSC_CTL(a) (-EINVAL)
  70. #endif
  71. #ifndef SET_TSC_CTL
  72. # define SET_TSC_CTL(a) (-EINVAL)
  73. #endif
  74. /*
  75. * this is where the system-wide overflow UID and GID are defined, for
  76. * architectures that now have 32-bit UID/GID but didn't in the past
  77. */
  78. int overflowuid = DEFAULT_OVERFLOWUID;
  79. int overflowgid = DEFAULT_OVERFLOWGID;
  80. #ifdef CONFIG_UID16
  81. EXPORT_SYMBOL(overflowuid);
  82. EXPORT_SYMBOL(overflowgid);
  83. #endif
  84. /*
  85. * the same as above, but for filesystems which can only store a 16-bit
  86. * UID and GID. as such, this is needed on all architectures
  87. */
  88. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  89. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  90. EXPORT_SYMBOL(fs_overflowuid);
  91. EXPORT_SYMBOL(fs_overflowgid);
  92. /*
  93. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  94. */
  95. int C_A_D = 1;
  96. struct pid *cad_pid;
  97. EXPORT_SYMBOL(cad_pid);
  98. /*
  99. * If set, this is used for preparing the system to power off.
  100. */
  101. void (*pm_power_off_prepare)(void);
  102. /*
  103. * set the priority of a task
  104. * - the caller must hold the RCU read lock
  105. */
  106. static int set_one_prio(struct task_struct *p, int niceval, int error)
  107. {
  108. const struct cred *cred = current_cred(), *pcred = __task_cred(p);
  109. int no_nice;
  110. if (pcred->uid != cred->euid &&
  111. pcred->euid != cred->euid && !capable(CAP_SYS_NICE)) {
  112. error = -EPERM;
  113. goto out;
  114. }
  115. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  116. error = -EACCES;
  117. goto out;
  118. }
  119. no_nice = security_task_setnice(p, niceval);
  120. if (no_nice) {
  121. error = no_nice;
  122. goto out;
  123. }
  124. if (error == -ESRCH)
  125. error = 0;
  126. set_user_nice(p, niceval);
  127. out:
  128. return error;
  129. }
  130. SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
  131. {
  132. struct task_struct *g, *p;
  133. struct user_struct *user;
  134. const struct cred *cred = current_cred();
  135. int error = -EINVAL;
  136. struct pid *pgrp;
  137. if (which > PRIO_USER || which < PRIO_PROCESS)
  138. goto out;
  139. /* normalize: avoid signed division (rounding problems) */
  140. error = -ESRCH;
  141. if (niceval < -20)
  142. niceval = -20;
  143. if (niceval > 19)
  144. niceval = 19;
  145. rcu_read_lock();
  146. read_lock(&tasklist_lock);
  147. switch (which) {
  148. case PRIO_PROCESS:
  149. if (who)
  150. p = find_task_by_vpid(who);
  151. else
  152. p = current;
  153. if (p)
  154. error = set_one_prio(p, niceval, error);
  155. break;
  156. case PRIO_PGRP:
  157. if (who)
  158. pgrp = find_vpid(who);
  159. else
  160. pgrp = task_pgrp(current);
  161. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  162. error = set_one_prio(p, niceval, error);
  163. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  164. break;
  165. case PRIO_USER:
  166. user = (struct user_struct *) cred->user;
  167. if (!who)
  168. who = cred->uid;
  169. else if ((who != cred->uid) &&
  170. !(user = find_user(who)))
  171. goto out_unlock; /* No processes for this user */
  172. do_each_thread(g, p) {
  173. if (__task_cred(p)->uid == who)
  174. error = set_one_prio(p, niceval, error);
  175. } while_each_thread(g, p);
  176. if (who != cred->uid)
  177. free_uid(user); /* For find_user() */
  178. break;
  179. }
  180. out_unlock:
  181. read_unlock(&tasklist_lock);
  182. rcu_read_unlock();
  183. out:
  184. return error;
  185. }
  186. /*
  187. * Ugh. To avoid negative return values, "getpriority()" will
  188. * not return the normal nice-value, but a negated value that
  189. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  190. * to stay compatible.
  191. */
  192. SYSCALL_DEFINE2(getpriority, int, which, int, who)
  193. {
  194. struct task_struct *g, *p;
  195. struct user_struct *user;
  196. const struct cred *cred = current_cred();
  197. long niceval, retval = -ESRCH;
  198. struct pid *pgrp;
  199. if (which > PRIO_USER || which < PRIO_PROCESS)
  200. return -EINVAL;
  201. rcu_read_lock();
  202. read_lock(&tasklist_lock);
  203. switch (which) {
  204. case PRIO_PROCESS:
  205. if (who)
  206. p = find_task_by_vpid(who);
  207. else
  208. p = current;
  209. if (p) {
  210. niceval = 20 - task_nice(p);
  211. if (niceval > retval)
  212. retval = niceval;
  213. }
  214. break;
  215. case PRIO_PGRP:
  216. if (who)
  217. pgrp = find_vpid(who);
  218. else
  219. pgrp = task_pgrp(current);
  220. do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
  221. niceval = 20 - task_nice(p);
  222. if (niceval > retval)
  223. retval = niceval;
  224. } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
  225. break;
  226. case PRIO_USER:
  227. user = (struct user_struct *) cred->user;
  228. if (!who)
  229. who = cred->uid;
  230. else if ((who != cred->uid) &&
  231. !(user = find_user(who)))
  232. goto out_unlock; /* No processes for this user */
  233. do_each_thread(g, p) {
  234. if (__task_cred(p)->uid == who) {
  235. niceval = 20 - task_nice(p);
  236. if (niceval > retval)
  237. retval = niceval;
  238. }
  239. } while_each_thread(g, p);
  240. if (who != cred->uid)
  241. free_uid(user); /* for find_user() */
  242. break;
  243. }
  244. out_unlock:
  245. read_unlock(&tasklist_lock);
  246. rcu_read_unlock();
  247. return retval;
  248. }
  249. /**
  250. * emergency_restart - reboot the system
  251. *
  252. * Without shutting down any hardware or taking any locks
  253. * reboot the system. This is called when we know we are in
  254. * trouble so this is our best effort to reboot. This is
  255. * safe to call in interrupt context.
  256. */
  257. void emergency_restart(void)
  258. {
  259. machine_emergency_restart();
  260. }
  261. EXPORT_SYMBOL_GPL(emergency_restart);
  262. void kernel_restart_prepare(char *cmd)
  263. {
  264. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  265. system_state = SYSTEM_RESTART;
  266. device_shutdown();
  267. sysdev_shutdown();
  268. }
  269. /**
  270. * kernel_restart - reboot the system
  271. * @cmd: pointer to buffer containing command to execute for restart
  272. * or %NULL
  273. *
  274. * Shutdown everything and perform a clean reboot.
  275. * This is not safe to call in interrupt context.
  276. */
  277. void kernel_restart(char *cmd)
  278. {
  279. kernel_restart_prepare(cmd);
  280. if (!cmd)
  281. printk(KERN_EMERG "Restarting system.\n");
  282. else
  283. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  284. machine_restart(cmd);
  285. }
  286. EXPORT_SYMBOL_GPL(kernel_restart);
  287. static void kernel_shutdown_prepare(enum system_states state)
  288. {
  289. blocking_notifier_call_chain(&reboot_notifier_list,
  290. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  291. system_state = state;
  292. device_shutdown();
  293. }
  294. /**
  295. * kernel_halt - halt the system
  296. *
  297. * Shutdown everything and perform a clean system halt.
  298. */
  299. void kernel_halt(void)
  300. {
  301. kernel_shutdown_prepare(SYSTEM_HALT);
  302. sysdev_shutdown();
  303. printk(KERN_EMERG "System halted.\n");
  304. machine_halt();
  305. }
  306. EXPORT_SYMBOL_GPL(kernel_halt);
  307. /**
  308. * kernel_power_off - power_off the system
  309. *
  310. * Shutdown everything and perform a clean system power_off.
  311. */
  312. void kernel_power_off(void)
  313. {
  314. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  315. if (pm_power_off_prepare)
  316. pm_power_off_prepare();
  317. disable_nonboot_cpus();
  318. sysdev_shutdown();
  319. printk(KERN_EMERG "Power down.\n");
  320. machine_power_off();
  321. }
  322. EXPORT_SYMBOL_GPL(kernel_power_off);
  323. static DEFINE_MUTEX(reboot_mutex);
  324. /*
  325. * Reboot system call: for obvious reasons only root may call it,
  326. * and even root needs to set up some magic numbers in the registers
  327. * so that some mistake won't make this reboot the whole machine.
  328. * You can also set the meaning of the ctrl-alt-del-key here.
  329. *
  330. * reboot doesn't sync: do that yourself before calling this.
  331. */
  332. SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd,
  333. void __user *, arg)
  334. {
  335. char buffer[256];
  336. int ret = 0;
  337. /* We only trust the superuser with rebooting the system. */
  338. if (!capable(CAP_SYS_BOOT))
  339. return -EPERM;
  340. /* For safety, we require "magic" arguments. */
  341. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  342. (magic2 != LINUX_REBOOT_MAGIC2 &&
  343. magic2 != LINUX_REBOOT_MAGIC2A &&
  344. magic2 != LINUX_REBOOT_MAGIC2B &&
  345. magic2 != LINUX_REBOOT_MAGIC2C))
  346. return -EINVAL;
  347. /* Instead of trying to make the power_off code look like
  348. * halt when pm_power_off is not set do it the easy way.
  349. */
  350. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  351. cmd = LINUX_REBOOT_CMD_HALT;
  352. mutex_lock(&reboot_mutex);
  353. switch (cmd) {
  354. case LINUX_REBOOT_CMD_RESTART:
  355. kernel_restart(NULL);
  356. break;
  357. case LINUX_REBOOT_CMD_CAD_ON:
  358. C_A_D = 1;
  359. break;
  360. case LINUX_REBOOT_CMD_CAD_OFF:
  361. C_A_D = 0;
  362. break;
  363. case LINUX_REBOOT_CMD_HALT:
  364. kernel_halt();
  365. do_exit(0);
  366. panic("cannot halt");
  367. case LINUX_REBOOT_CMD_POWER_OFF:
  368. kernel_power_off();
  369. do_exit(0);
  370. break;
  371. case LINUX_REBOOT_CMD_RESTART2:
  372. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  373. ret = -EFAULT;
  374. break;
  375. }
  376. buffer[sizeof(buffer) - 1] = '\0';
  377. kernel_restart(buffer);
  378. break;
  379. #ifdef CONFIG_KEXEC
  380. case LINUX_REBOOT_CMD_KEXEC:
  381. ret = kernel_kexec();
  382. break;
  383. #endif
  384. #ifdef CONFIG_HIBERNATION
  385. case LINUX_REBOOT_CMD_SW_SUSPEND:
  386. ret = hibernate();
  387. break;
  388. #endif
  389. default:
  390. ret = -EINVAL;
  391. break;
  392. }
  393. mutex_unlock(&reboot_mutex);
  394. return ret;
  395. }
  396. static void deferred_cad(struct work_struct *dummy)
  397. {
  398. kernel_restart(NULL);
  399. }
  400. /*
  401. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  402. * As it's called within an interrupt, it may NOT sync: the only choice
  403. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  404. */
  405. void ctrl_alt_del(void)
  406. {
  407. static DECLARE_WORK(cad_work, deferred_cad);
  408. if (C_A_D)
  409. schedule_work(&cad_work);
  410. else
  411. kill_cad_pid(SIGINT, 1);
  412. }
  413. /*
  414. * Unprivileged users may change the real gid to the effective gid
  415. * or vice versa. (BSD-style)
  416. *
  417. * If you set the real gid at all, or set the effective gid to a value not
  418. * equal to the real gid, then the saved gid is set to the new effective gid.
  419. *
  420. * This makes it possible for a setgid program to completely drop its
  421. * privileges, which is often a useful assertion to make when you are doing
  422. * a security audit over a program.
  423. *
  424. * The general idea is that a program which uses just setregid() will be
  425. * 100% compatible with BSD. A program which uses just setgid() will be
  426. * 100% compatible with POSIX with saved IDs.
  427. *
  428. * SMP: There are not races, the GIDs are checked only by filesystem
  429. * operations (as far as semantic preservation is concerned).
  430. */
  431. SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
  432. {
  433. const struct cred *old;
  434. struct cred *new;
  435. int retval;
  436. new = prepare_creds();
  437. if (!new)
  438. return -ENOMEM;
  439. old = current_cred();
  440. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  441. if (retval)
  442. goto error;
  443. retval = -EPERM;
  444. if (rgid != (gid_t) -1) {
  445. if (old->gid == rgid ||
  446. old->egid == rgid ||
  447. capable(CAP_SETGID))
  448. new->gid = rgid;
  449. else
  450. goto error;
  451. }
  452. if (egid != (gid_t) -1) {
  453. if (old->gid == egid ||
  454. old->egid == egid ||
  455. old->sgid == egid ||
  456. capable(CAP_SETGID))
  457. new->egid = egid;
  458. else
  459. goto error;
  460. }
  461. if (rgid != (gid_t) -1 ||
  462. (egid != (gid_t) -1 && egid != old->gid))
  463. new->sgid = new->egid;
  464. new->fsgid = new->egid;
  465. return commit_creds(new);
  466. error:
  467. abort_creds(new);
  468. return retval;
  469. }
  470. /*
  471. * setgid() is implemented like SysV w/ SAVED_IDS
  472. *
  473. * SMP: Same implicit races as above.
  474. */
  475. SYSCALL_DEFINE1(setgid, gid_t, gid)
  476. {
  477. const struct cred *old;
  478. struct cred *new;
  479. int retval;
  480. new = prepare_creds();
  481. if (!new)
  482. return -ENOMEM;
  483. old = current_cred();
  484. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  485. if (retval)
  486. goto error;
  487. retval = -EPERM;
  488. if (capable(CAP_SETGID))
  489. new->gid = new->egid = new->sgid = new->fsgid = gid;
  490. else if (gid == old->gid || gid == old->sgid)
  491. new->egid = new->fsgid = gid;
  492. else
  493. goto error;
  494. return commit_creds(new);
  495. error:
  496. abort_creds(new);
  497. return retval;
  498. }
  499. /*
  500. * change the user struct in a credentials set to match the new UID
  501. */
  502. static int set_user(struct cred *new)
  503. {
  504. struct user_struct *new_user;
  505. new_user = alloc_uid(current_user_ns(), new->uid);
  506. if (!new_user)
  507. return -EAGAIN;
  508. if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) &&
  509. new_user != INIT_USER) {
  510. free_uid(new_user);
  511. return -EAGAIN;
  512. }
  513. free_uid(new->user);
  514. new->user = new_user;
  515. return 0;
  516. }
  517. /*
  518. * Unprivileged users may change the real uid to the effective uid
  519. * or vice versa. (BSD-style)
  520. *
  521. * If you set the real uid at all, or set the effective uid to a value not
  522. * equal to the real uid, then the saved uid is set to the new effective uid.
  523. *
  524. * This makes it possible for a setuid program to completely drop its
  525. * privileges, which is often a useful assertion to make when you are doing
  526. * a security audit over a program.
  527. *
  528. * The general idea is that a program which uses just setreuid() will be
  529. * 100% compatible with BSD. A program which uses just setuid() will be
  530. * 100% compatible with POSIX with saved IDs.
  531. */
  532. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  533. {
  534. const struct cred *old;
  535. struct cred *new;
  536. int retval;
  537. new = prepare_creds();
  538. if (!new)
  539. return -ENOMEM;
  540. old = current_cred();
  541. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  542. if (retval)
  543. goto error;
  544. retval = -EPERM;
  545. if (ruid != (uid_t) -1) {
  546. new->uid = ruid;
  547. if (old->uid != ruid &&
  548. old->euid != ruid &&
  549. !capable(CAP_SETUID))
  550. goto error;
  551. }
  552. if (euid != (uid_t) -1) {
  553. new->euid = euid;
  554. if (old->uid != euid &&
  555. old->euid != euid &&
  556. old->suid != euid &&
  557. !capable(CAP_SETUID))
  558. goto error;
  559. }
  560. if (new->uid != old->uid) {
  561. retval = set_user(new);
  562. if (retval < 0)
  563. goto error;
  564. }
  565. if (ruid != (uid_t) -1 ||
  566. (euid != (uid_t) -1 && euid != old->uid))
  567. new->suid = new->euid;
  568. new->fsuid = new->euid;
  569. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  570. if (retval < 0)
  571. goto error;
  572. return commit_creds(new);
  573. error:
  574. abort_creds(new);
  575. return retval;
  576. }
  577. /*
  578. * setuid() is implemented like SysV with SAVED_IDS
  579. *
  580. * Note that SAVED_ID's is deficient in that a setuid root program
  581. * like sendmail, for example, cannot set its uid to be a normal
  582. * user and then switch back, because if you're root, setuid() sets
  583. * the saved uid too. If you don't like this, blame the bright people
  584. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  585. * will allow a root program to temporarily drop privileges and be able to
  586. * regain them by swapping the real and effective uid.
  587. */
  588. SYSCALL_DEFINE1(setuid, uid_t, uid)
  589. {
  590. const struct cred *old;
  591. struct cred *new;
  592. int retval;
  593. new = prepare_creds();
  594. if (!new)
  595. return -ENOMEM;
  596. old = current_cred();
  597. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  598. if (retval)
  599. goto error;
  600. retval = -EPERM;
  601. if (capable(CAP_SETUID)) {
  602. new->suid = new->uid = uid;
  603. if (uid != old->uid) {
  604. retval = set_user(new);
  605. if (retval < 0)
  606. goto error;
  607. }
  608. } else if (uid != old->uid && uid != new->suid) {
  609. goto error;
  610. }
  611. new->fsuid = new->euid = uid;
  612. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  613. if (retval < 0)
  614. goto error;
  615. return commit_creds(new);
  616. error:
  617. abort_creds(new);
  618. return retval;
  619. }
  620. /*
  621. * This function implements a generic ability to update ruid, euid,
  622. * and suid. This allows you to implement the 4.4 compatible seteuid().
  623. */
  624. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  625. {
  626. const struct cred *old;
  627. struct cred *new;
  628. int retval;
  629. new = prepare_creds();
  630. if (!new)
  631. return -ENOMEM;
  632. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  633. if (retval)
  634. goto error;
  635. old = current_cred();
  636. retval = -EPERM;
  637. if (!capable(CAP_SETUID)) {
  638. if (ruid != (uid_t) -1 && ruid != old->uid &&
  639. ruid != old->euid && ruid != old->suid)
  640. goto error;
  641. if (euid != (uid_t) -1 && euid != old->uid &&
  642. euid != old->euid && euid != old->suid)
  643. goto error;
  644. if (suid != (uid_t) -1 && suid != old->uid &&
  645. suid != old->euid && suid != old->suid)
  646. goto error;
  647. }
  648. if (ruid != (uid_t) -1) {
  649. new->uid = ruid;
  650. if (ruid != old->uid) {
  651. retval = set_user(new);
  652. if (retval < 0)
  653. goto error;
  654. }
  655. }
  656. if (euid != (uid_t) -1)
  657. new->euid = euid;
  658. if (suid != (uid_t) -1)
  659. new->suid = suid;
  660. new->fsuid = new->euid;
  661. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  662. if (retval < 0)
  663. goto error;
  664. return commit_creds(new);
  665. error:
  666. abort_creds(new);
  667. return retval;
  668. }
  669. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  670. {
  671. const struct cred *cred = current_cred();
  672. int retval;
  673. if (!(retval = put_user(cred->uid, ruid)) &&
  674. !(retval = put_user(cred->euid, euid)))
  675. retval = put_user(cred->suid, suid);
  676. return retval;
  677. }
  678. /*
  679. * Same as above, but for rgid, egid, sgid.
  680. */
  681. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  682. {
  683. const struct cred *old;
  684. struct cred *new;
  685. int retval;
  686. new = prepare_creds();
  687. if (!new)
  688. return -ENOMEM;
  689. old = current_cred();
  690. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  691. if (retval)
  692. goto error;
  693. retval = -EPERM;
  694. if (!capable(CAP_SETGID)) {
  695. if (rgid != (gid_t) -1 && rgid != old->gid &&
  696. rgid != old->egid && rgid != old->sgid)
  697. goto error;
  698. if (egid != (gid_t) -1 && egid != old->gid &&
  699. egid != old->egid && egid != old->sgid)
  700. goto error;
  701. if (sgid != (gid_t) -1 && sgid != old->gid &&
  702. sgid != old->egid && sgid != old->sgid)
  703. goto error;
  704. }
  705. if (rgid != (gid_t) -1)
  706. new->gid = rgid;
  707. if (egid != (gid_t) -1)
  708. new->egid = egid;
  709. if (sgid != (gid_t) -1)
  710. new->sgid = sgid;
  711. new->fsgid = new->egid;
  712. return commit_creds(new);
  713. error:
  714. abort_creds(new);
  715. return retval;
  716. }
  717. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  718. {
  719. const struct cred *cred = current_cred();
  720. int retval;
  721. if (!(retval = put_user(cred->gid, rgid)) &&
  722. !(retval = put_user(cred->egid, egid)))
  723. retval = put_user(cred->sgid, sgid);
  724. return retval;
  725. }
  726. /*
  727. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  728. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  729. * whatever uid it wants to). It normally shadows "euid", except when
  730. * explicitly set by setfsuid() or for access..
  731. */
  732. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  733. {
  734. const struct cred *old;
  735. struct cred *new;
  736. uid_t old_fsuid;
  737. new = prepare_creds();
  738. if (!new)
  739. return current_fsuid();
  740. old = current_cred();
  741. old_fsuid = old->fsuid;
  742. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
  743. goto error;
  744. if (uid == old->uid || uid == old->euid ||
  745. uid == old->suid || uid == old->fsuid ||
  746. capable(CAP_SETUID)) {
  747. if (uid != old_fsuid) {
  748. new->fsuid = uid;
  749. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  750. goto change_okay;
  751. }
  752. }
  753. error:
  754. abort_creds(new);
  755. return old_fsuid;
  756. change_okay:
  757. commit_creds(new);
  758. return old_fsuid;
  759. }
  760. /*
  761. * Samma på svenska..
  762. */
  763. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  764. {
  765. const struct cred *old;
  766. struct cred *new;
  767. gid_t old_fsgid;
  768. new = prepare_creds();
  769. if (!new)
  770. return current_fsgid();
  771. old = current_cred();
  772. old_fsgid = old->fsgid;
  773. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  774. goto error;
  775. if (gid == old->gid || gid == old->egid ||
  776. gid == old->sgid || gid == old->fsgid ||
  777. capable(CAP_SETGID)) {
  778. if (gid != old_fsgid) {
  779. new->fsgid = gid;
  780. goto change_okay;
  781. }
  782. }
  783. error:
  784. abort_creds(new);
  785. return old_fsgid;
  786. change_okay:
  787. commit_creds(new);
  788. return old_fsgid;
  789. }
  790. void do_sys_times(struct tms *tms)
  791. {
  792. cputime_t tgutime, tgstime, cutime, cstime;
  793. spin_lock_irq(&current->sighand->siglock);
  794. thread_group_times(current, &tgutime, &tgstime);
  795. cutime = current->signal->cutime;
  796. cstime = current->signal->cstime;
  797. spin_unlock_irq(&current->sighand->siglock);
  798. tms->tms_utime = cputime_to_clock_t(tgutime);
  799. tms->tms_stime = cputime_to_clock_t(tgstime);
  800. tms->tms_cutime = cputime_to_clock_t(cutime);
  801. tms->tms_cstime = cputime_to_clock_t(cstime);
  802. }
  803. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  804. {
  805. if (tbuf) {
  806. struct tms tmp;
  807. do_sys_times(&tmp);
  808. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  809. return -EFAULT;
  810. }
  811. force_successful_syscall_return();
  812. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  813. }
  814. /*
  815. * This needs some heavy checking ...
  816. * I just haven't the stomach for it. I also don't fully
  817. * understand sessions/pgrp etc. Let somebody who does explain it.
  818. *
  819. * OK, I think I have the protection semantics right.... this is really
  820. * only important on a multi-user system anyway, to make sure one user
  821. * can't send a signal to a process owned by another. -TYT, 12/12/91
  822. *
  823. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  824. * LBT 04.03.94
  825. */
  826. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  827. {
  828. struct task_struct *p;
  829. struct task_struct *group_leader = current->group_leader;
  830. struct pid *pgrp;
  831. int err;
  832. if (!pid)
  833. pid = task_pid_vnr(group_leader);
  834. if (!pgid)
  835. pgid = pid;
  836. if (pgid < 0)
  837. return -EINVAL;
  838. /* From this point forward we keep holding onto the tasklist lock
  839. * so that our parent does not change from under us. -DaveM
  840. */
  841. write_lock_irq(&tasklist_lock);
  842. err = -ESRCH;
  843. p = find_task_by_vpid(pid);
  844. if (!p)
  845. goto out;
  846. err = -EINVAL;
  847. if (!thread_group_leader(p))
  848. goto out;
  849. if (same_thread_group(p->real_parent, group_leader)) {
  850. err = -EPERM;
  851. if (task_session(p) != task_session(group_leader))
  852. goto out;
  853. err = -EACCES;
  854. if (p->did_exec)
  855. goto out;
  856. } else {
  857. err = -ESRCH;
  858. if (p != group_leader)
  859. goto out;
  860. }
  861. err = -EPERM;
  862. if (p->signal->leader)
  863. goto out;
  864. pgrp = task_pid(p);
  865. if (pgid != pid) {
  866. struct task_struct *g;
  867. pgrp = find_vpid(pgid);
  868. g = pid_task(pgrp, PIDTYPE_PGID);
  869. if (!g || task_session(g) != task_session(group_leader))
  870. goto out;
  871. }
  872. err = security_task_setpgid(p, pgid);
  873. if (err)
  874. goto out;
  875. if (task_pgrp(p) != pgrp)
  876. change_pid(p, PIDTYPE_PGID, pgrp);
  877. err = 0;
  878. out:
  879. /* All paths lead to here, thus we are safe. -DaveM */
  880. write_unlock_irq(&tasklist_lock);
  881. return err;
  882. }
  883. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  884. {
  885. struct task_struct *p;
  886. struct pid *grp;
  887. int retval;
  888. rcu_read_lock();
  889. if (!pid)
  890. grp = task_pgrp(current);
  891. else {
  892. retval = -ESRCH;
  893. p = find_task_by_vpid(pid);
  894. if (!p)
  895. goto out;
  896. grp = task_pgrp(p);
  897. if (!grp)
  898. goto out;
  899. retval = security_task_getpgid(p);
  900. if (retval)
  901. goto out;
  902. }
  903. retval = pid_vnr(grp);
  904. out:
  905. rcu_read_unlock();
  906. return retval;
  907. }
  908. #ifdef __ARCH_WANT_SYS_GETPGRP
  909. SYSCALL_DEFINE0(getpgrp)
  910. {
  911. return sys_getpgid(0);
  912. }
  913. #endif
  914. SYSCALL_DEFINE1(getsid, pid_t, pid)
  915. {
  916. struct task_struct *p;
  917. struct pid *sid;
  918. int retval;
  919. rcu_read_lock();
  920. if (!pid)
  921. sid = task_session(current);
  922. else {
  923. retval = -ESRCH;
  924. p = find_task_by_vpid(pid);
  925. if (!p)
  926. goto out;
  927. sid = task_session(p);
  928. if (!sid)
  929. goto out;
  930. retval = security_task_getsid(p);
  931. if (retval)
  932. goto out;
  933. }
  934. retval = pid_vnr(sid);
  935. out:
  936. rcu_read_unlock();
  937. return retval;
  938. }
  939. SYSCALL_DEFINE0(setsid)
  940. {
  941. struct task_struct *group_leader = current->group_leader;
  942. struct pid *sid = task_pid(group_leader);
  943. pid_t session = pid_vnr(sid);
  944. int err = -EPERM;
  945. write_lock_irq(&tasklist_lock);
  946. /* Fail if I am already a session leader */
  947. if (group_leader->signal->leader)
  948. goto out;
  949. /* Fail if a process group id already exists that equals the
  950. * proposed session id.
  951. */
  952. if (pid_task(sid, PIDTYPE_PGID))
  953. goto out;
  954. group_leader->signal->leader = 1;
  955. __set_special_pids(sid);
  956. proc_clear_tty(group_leader);
  957. err = session;
  958. out:
  959. write_unlock_irq(&tasklist_lock);
  960. if (err > 0)
  961. proc_sid_connector(group_leader);
  962. return err;
  963. }
  964. DECLARE_RWSEM(uts_sem);
  965. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  966. {
  967. int errno = 0;
  968. down_read(&uts_sem);
  969. if (copy_to_user(name, utsname(), sizeof *name))
  970. errno = -EFAULT;
  971. up_read(&uts_sem);
  972. return errno;
  973. }
  974. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  975. {
  976. int errno;
  977. char tmp[__NEW_UTS_LEN];
  978. if (!capable(CAP_SYS_ADMIN))
  979. return -EPERM;
  980. if (len < 0 || len > __NEW_UTS_LEN)
  981. return -EINVAL;
  982. down_write(&uts_sem);
  983. errno = -EFAULT;
  984. if (!copy_from_user(tmp, name, len)) {
  985. struct new_utsname *u = utsname();
  986. memcpy(u->nodename, tmp, len);
  987. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  988. errno = 0;
  989. }
  990. up_write(&uts_sem);
  991. return errno;
  992. }
  993. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  994. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  995. {
  996. int i, errno;
  997. struct new_utsname *u;
  998. if (len < 0)
  999. return -EINVAL;
  1000. down_read(&uts_sem);
  1001. u = utsname();
  1002. i = 1 + strlen(u->nodename);
  1003. if (i > len)
  1004. i = len;
  1005. errno = 0;
  1006. if (copy_to_user(name, u->nodename, i))
  1007. errno = -EFAULT;
  1008. up_read(&uts_sem);
  1009. return errno;
  1010. }
  1011. #endif
  1012. /*
  1013. * Only setdomainname; getdomainname can be implemented by calling
  1014. * uname()
  1015. */
  1016. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1017. {
  1018. int errno;
  1019. char tmp[__NEW_UTS_LEN];
  1020. if (!capable(CAP_SYS_ADMIN))
  1021. return -EPERM;
  1022. if (len < 0 || len > __NEW_UTS_LEN)
  1023. return -EINVAL;
  1024. down_write(&uts_sem);
  1025. errno = -EFAULT;
  1026. if (!copy_from_user(tmp, name, len)) {
  1027. struct new_utsname *u = utsname();
  1028. memcpy(u->domainname, tmp, len);
  1029. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1030. errno = 0;
  1031. }
  1032. up_write(&uts_sem);
  1033. return errno;
  1034. }
  1035. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1036. {
  1037. if (resource >= RLIM_NLIMITS)
  1038. return -EINVAL;
  1039. else {
  1040. struct rlimit value;
  1041. task_lock(current->group_leader);
  1042. value = current->signal->rlim[resource];
  1043. task_unlock(current->group_leader);
  1044. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1045. }
  1046. }
  1047. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1048. /*
  1049. * Back compatibility for getrlimit. Needed for some apps.
  1050. */
  1051. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1052. struct rlimit __user *, rlim)
  1053. {
  1054. struct rlimit x;
  1055. if (resource >= RLIM_NLIMITS)
  1056. return -EINVAL;
  1057. task_lock(current->group_leader);
  1058. x = current->signal->rlim[resource];
  1059. task_unlock(current->group_leader);
  1060. if (x.rlim_cur > 0x7FFFFFFF)
  1061. x.rlim_cur = 0x7FFFFFFF;
  1062. if (x.rlim_max > 0x7FFFFFFF)
  1063. x.rlim_max = 0x7FFFFFFF;
  1064. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1065. }
  1066. #endif
  1067. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1068. {
  1069. struct rlimit new_rlim, *old_rlim;
  1070. int retval;
  1071. if (resource >= RLIM_NLIMITS)
  1072. return -EINVAL;
  1073. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1074. return -EFAULT;
  1075. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1076. return -EINVAL;
  1077. old_rlim = current->signal->rlim + resource;
  1078. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1079. !capable(CAP_SYS_RESOURCE))
  1080. return -EPERM;
  1081. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1082. return -EPERM;
  1083. retval = security_task_setrlimit(resource, &new_rlim);
  1084. if (retval)
  1085. return retval;
  1086. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1087. /*
  1088. * The caller is asking for an immediate RLIMIT_CPU
  1089. * expiry. But we use the zero value to mean "it was
  1090. * never set". So let's cheat and make it one second
  1091. * instead
  1092. */
  1093. new_rlim.rlim_cur = 1;
  1094. }
  1095. task_lock(current->group_leader);
  1096. *old_rlim = new_rlim;
  1097. task_unlock(current->group_leader);
  1098. if (resource != RLIMIT_CPU)
  1099. goto out;
  1100. /*
  1101. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1102. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1103. * very long-standing error, and fixing it now risks breakage of
  1104. * applications, so we live with it
  1105. */
  1106. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1107. goto out;
  1108. update_rlimit_cpu(new_rlim.rlim_cur);
  1109. out:
  1110. return 0;
  1111. }
  1112. /*
  1113. * It would make sense to put struct rusage in the task_struct,
  1114. * except that would make the task_struct be *really big*. After
  1115. * task_struct gets moved into malloc'ed memory, it would
  1116. * make sense to do this. It will make moving the rest of the information
  1117. * a lot simpler! (Which we're not doing right now because we're not
  1118. * measuring them yet).
  1119. *
  1120. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1121. * races with threads incrementing their own counters. But since word
  1122. * reads are atomic, we either get new values or old values and we don't
  1123. * care which for the sums. We always take the siglock to protect reading
  1124. * the c* fields from p->signal from races with exit.c updating those
  1125. * fields when reaping, so a sample either gets all the additions of a
  1126. * given child after it's reaped, or none so this sample is before reaping.
  1127. *
  1128. * Locking:
  1129. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1130. * for the cases current multithreaded, non-current single threaded
  1131. * non-current multithreaded. Thread traversal is now safe with
  1132. * the siglock held.
  1133. * Strictly speaking, we donot need to take the siglock if we are current and
  1134. * single threaded, as no one else can take our signal_struct away, no one
  1135. * else can reap the children to update signal->c* counters, and no one else
  1136. * can race with the signal-> fields. If we do not take any lock, the
  1137. * signal-> fields could be read out of order while another thread was just
  1138. * exiting. So we should place a read memory barrier when we avoid the lock.
  1139. * On the writer side, write memory barrier is implied in __exit_signal
  1140. * as __exit_signal releases the siglock spinlock after updating the signal->
  1141. * fields. But we don't do this yet to keep things simple.
  1142. *
  1143. */
  1144. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1145. {
  1146. r->ru_nvcsw += t->nvcsw;
  1147. r->ru_nivcsw += t->nivcsw;
  1148. r->ru_minflt += t->min_flt;
  1149. r->ru_majflt += t->maj_flt;
  1150. r->ru_inblock += task_io_get_inblock(t);
  1151. r->ru_oublock += task_io_get_oublock(t);
  1152. }
  1153. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1154. {
  1155. struct task_struct *t;
  1156. unsigned long flags;
  1157. cputime_t tgutime, tgstime, utime, stime;
  1158. unsigned long maxrss = 0;
  1159. memset((char *) r, 0, sizeof *r);
  1160. utime = stime = cputime_zero;
  1161. if (who == RUSAGE_THREAD) {
  1162. task_times(current, &utime, &stime);
  1163. accumulate_thread_rusage(p, r);
  1164. maxrss = p->signal->maxrss;
  1165. goto out;
  1166. }
  1167. if (!lock_task_sighand(p, &flags))
  1168. return;
  1169. switch (who) {
  1170. case RUSAGE_BOTH:
  1171. case RUSAGE_CHILDREN:
  1172. utime = p->signal->cutime;
  1173. stime = p->signal->cstime;
  1174. r->ru_nvcsw = p->signal->cnvcsw;
  1175. r->ru_nivcsw = p->signal->cnivcsw;
  1176. r->ru_minflt = p->signal->cmin_flt;
  1177. r->ru_majflt = p->signal->cmaj_flt;
  1178. r->ru_inblock = p->signal->cinblock;
  1179. r->ru_oublock = p->signal->coublock;
  1180. maxrss = p->signal->cmaxrss;
  1181. if (who == RUSAGE_CHILDREN)
  1182. break;
  1183. case RUSAGE_SELF:
  1184. thread_group_times(p, &tgutime, &tgstime);
  1185. utime = cputime_add(utime, tgutime);
  1186. stime = cputime_add(stime, tgstime);
  1187. r->ru_nvcsw += p->signal->nvcsw;
  1188. r->ru_nivcsw += p->signal->nivcsw;
  1189. r->ru_minflt += p->signal->min_flt;
  1190. r->ru_majflt += p->signal->maj_flt;
  1191. r->ru_inblock += p->signal->inblock;
  1192. r->ru_oublock += p->signal->oublock;
  1193. if (maxrss < p->signal->maxrss)
  1194. maxrss = p->signal->maxrss;
  1195. t = p;
  1196. do {
  1197. accumulate_thread_rusage(t, r);
  1198. t = next_thread(t);
  1199. } while (t != p);
  1200. break;
  1201. default:
  1202. BUG();
  1203. }
  1204. unlock_task_sighand(p, &flags);
  1205. out:
  1206. cputime_to_timeval(utime, &r->ru_utime);
  1207. cputime_to_timeval(stime, &r->ru_stime);
  1208. if (who != RUSAGE_CHILDREN) {
  1209. struct mm_struct *mm = get_task_mm(p);
  1210. if (mm) {
  1211. setmax_mm_hiwater_rss(&maxrss, mm);
  1212. mmput(mm);
  1213. }
  1214. }
  1215. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1216. }
  1217. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1218. {
  1219. struct rusage r;
  1220. k_getrusage(p, who, &r);
  1221. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1222. }
  1223. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1224. {
  1225. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1226. who != RUSAGE_THREAD)
  1227. return -EINVAL;
  1228. return getrusage(current, who, ru);
  1229. }
  1230. SYSCALL_DEFINE1(umask, int, mask)
  1231. {
  1232. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1233. return mask;
  1234. }
  1235. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1236. unsigned long, arg4, unsigned long, arg5)
  1237. {
  1238. struct task_struct *me = current;
  1239. unsigned char comm[sizeof(me->comm)];
  1240. long error;
  1241. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1242. if (error != -ENOSYS)
  1243. return error;
  1244. error = 0;
  1245. switch (option) {
  1246. case PR_SET_PDEATHSIG:
  1247. if (!valid_signal(arg2)) {
  1248. error = -EINVAL;
  1249. break;
  1250. }
  1251. me->pdeath_signal = arg2;
  1252. error = 0;
  1253. break;
  1254. case PR_GET_PDEATHSIG:
  1255. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1256. break;
  1257. case PR_GET_DUMPABLE:
  1258. error = get_dumpable(me->mm);
  1259. break;
  1260. case PR_SET_DUMPABLE:
  1261. if (arg2 < 0 || arg2 > 1) {
  1262. error = -EINVAL;
  1263. break;
  1264. }
  1265. set_dumpable(me->mm, arg2);
  1266. error = 0;
  1267. break;
  1268. case PR_SET_UNALIGN:
  1269. error = SET_UNALIGN_CTL(me, arg2);
  1270. break;
  1271. case PR_GET_UNALIGN:
  1272. error = GET_UNALIGN_CTL(me, arg2);
  1273. break;
  1274. case PR_SET_FPEMU:
  1275. error = SET_FPEMU_CTL(me, arg2);
  1276. break;
  1277. case PR_GET_FPEMU:
  1278. error = GET_FPEMU_CTL(me, arg2);
  1279. break;
  1280. case PR_SET_FPEXC:
  1281. error = SET_FPEXC_CTL(me, arg2);
  1282. break;
  1283. case PR_GET_FPEXC:
  1284. error = GET_FPEXC_CTL(me, arg2);
  1285. break;
  1286. case PR_GET_TIMING:
  1287. error = PR_TIMING_STATISTICAL;
  1288. break;
  1289. case PR_SET_TIMING:
  1290. if (arg2 != PR_TIMING_STATISTICAL)
  1291. error = -EINVAL;
  1292. else
  1293. error = 0;
  1294. break;
  1295. case PR_SET_NAME:
  1296. comm[sizeof(me->comm)-1] = 0;
  1297. if (strncpy_from_user(comm, (char __user *)arg2,
  1298. sizeof(me->comm) - 1) < 0)
  1299. return -EFAULT;
  1300. set_task_comm(me, comm);
  1301. return 0;
  1302. case PR_GET_NAME:
  1303. get_task_comm(comm, me);
  1304. if (copy_to_user((char __user *)arg2, comm,
  1305. sizeof(comm)))
  1306. return -EFAULT;
  1307. return 0;
  1308. case PR_GET_ENDIAN:
  1309. error = GET_ENDIAN(me, arg2);
  1310. break;
  1311. case PR_SET_ENDIAN:
  1312. error = SET_ENDIAN(me, arg2);
  1313. break;
  1314. case PR_GET_SECCOMP:
  1315. error = prctl_get_seccomp();
  1316. break;
  1317. case PR_SET_SECCOMP:
  1318. error = prctl_set_seccomp(arg2);
  1319. break;
  1320. case PR_GET_TSC:
  1321. error = GET_TSC_CTL(arg2);
  1322. break;
  1323. case PR_SET_TSC:
  1324. error = SET_TSC_CTL(arg2);
  1325. break;
  1326. case PR_TASK_PERF_EVENTS_DISABLE:
  1327. error = perf_event_task_disable();
  1328. break;
  1329. case PR_TASK_PERF_EVENTS_ENABLE:
  1330. error = perf_event_task_enable();
  1331. break;
  1332. case PR_GET_TIMERSLACK:
  1333. error = current->timer_slack_ns;
  1334. break;
  1335. case PR_SET_TIMERSLACK:
  1336. if (arg2 <= 0)
  1337. current->timer_slack_ns =
  1338. current->default_timer_slack_ns;
  1339. else
  1340. current->timer_slack_ns = arg2;
  1341. error = 0;
  1342. break;
  1343. case PR_MCE_KILL:
  1344. if (arg4 | arg5)
  1345. return -EINVAL;
  1346. switch (arg2) {
  1347. case PR_MCE_KILL_CLEAR:
  1348. if (arg3 != 0)
  1349. return -EINVAL;
  1350. current->flags &= ~PF_MCE_PROCESS;
  1351. break;
  1352. case PR_MCE_KILL_SET:
  1353. current->flags |= PF_MCE_PROCESS;
  1354. if (arg3 == PR_MCE_KILL_EARLY)
  1355. current->flags |= PF_MCE_EARLY;
  1356. else if (arg3 == PR_MCE_KILL_LATE)
  1357. current->flags &= ~PF_MCE_EARLY;
  1358. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1359. current->flags &=
  1360. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1361. else
  1362. return -EINVAL;
  1363. break;
  1364. default:
  1365. return -EINVAL;
  1366. }
  1367. error = 0;
  1368. break;
  1369. case PR_MCE_KILL_GET:
  1370. if (arg2 | arg3 | arg4 | arg5)
  1371. return -EINVAL;
  1372. if (current->flags & PF_MCE_PROCESS)
  1373. error = (current->flags & PF_MCE_EARLY) ?
  1374. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1375. else
  1376. error = PR_MCE_KILL_DEFAULT;
  1377. break;
  1378. default:
  1379. error = -EINVAL;
  1380. break;
  1381. }
  1382. return error;
  1383. }
  1384. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1385. struct getcpu_cache __user *, unused)
  1386. {
  1387. int err = 0;
  1388. int cpu = raw_smp_processor_id();
  1389. if (cpup)
  1390. err |= put_user(cpu, cpup);
  1391. if (nodep)
  1392. err |= put_user(cpu_to_node(cpu), nodep);
  1393. return err ? -EFAULT : 0;
  1394. }
  1395. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1396. static void argv_cleanup(char **argv, char **envp)
  1397. {
  1398. argv_free(argv);
  1399. }
  1400. /**
  1401. * orderly_poweroff - Trigger an orderly system poweroff
  1402. * @force: force poweroff if command execution fails
  1403. *
  1404. * This may be called from any context to trigger a system shutdown.
  1405. * If the orderly shutdown fails, it will force an immediate shutdown.
  1406. */
  1407. int orderly_poweroff(bool force)
  1408. {
  1409. int argc;
  1410. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1411. static char *envp[] = {
  1412. "HOME=/",
  1413. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1414. NULL
  1415. };
  1416. int ret = -ENOMEM;
  1417. struct subprocess_info *info;
  1418. if (argv == NULL) {
  1419. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1420. __func__, poweroff_cmd);
  1421. goto out;
  1422. }
  1423. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1424. if (info == NULL) {
  1425. argv_free(argv);
  1426. goto out;
  1427. }
  1428. call_usermodehelper_setcleanup(info, argv_cleanup);
  1429. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1430. out:
  1431. if (ret && force) {
  1432. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1433. "forcing the issue\n");
  1434. /* I guess this should try to kick off some daemon to
  1435. sync and poweroff asap. Or not even bother syncing
  1436. if we're doing an emergency shutdown? */
  1437. emergency_sync();
  1438. kernel_power_off();
  1439. }
  1440. return ret;
  1441. }
  1442. EXPORT_SYMBOL_GPL(orderly_poweroff);