sys.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  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 (!task_can_switch_user(new_user, current)) {
  509. free_uid(new_user);
  510. return -EINVAL;
  511. }
  512. if (atomic_read(&new_user->processes) >=
  513. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  514. new_user != INIT_USER) {
  515. free_uid(new_user);
  516. return -EAGAIN;
  517. }
  518. free_uid(new->user);
  519. new->user = new_user;
  520. return 0;
  521. }
  522. /*
  523. * Unprivileged users may change the real uid to the effective uid
  524. * or vice versa. (BSD-style)
  525. *
  526. * If you set the real uid at all, or set the effective uid to a value not
  527. * equal to the real uid, then the saved uid is set to the new effective uid.
  528. *
  529. * This makes it possible for a setuid program to completely drop its
  530. * privileges, which is often a useful assertion to make when you are doing
  531. * a security audit over a program.
  532. *
  533. * The general idea is that a program which uses just setreuid() will be
  534. * 100% compatible with BSD. A program which uses just setuid() will be
  535. * 100% compatible with POSIX with saved IDs.
  536. */
  537. SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
  538. {
  539. const struct cred *old;
  540. struct cred *new;
  541. int retval;
  542. new = prepare_creds();
  543. if (!new)
  544. return -ENOMEM;
  545. old = current_cred();
  546. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  547. if (retval)
  548. goto error;
  549. retval = -EPERM;
  550. if (ruid != (uid_t) -1) {
  551. new->uid = ruid;
  552. if (old->uid != ruid &&
  553. old->euid != ruid &&
  554. !capable(CAP_SETUID))
  555. goto error;
  556. }
  557. if (euid != (uid_t) -1) {
  558. new->euid = euid;
  559. if (old->uid != euid &&
  560. old->euid != euid &&
  561. old->suid != euid &&
  562. !capable(CAP_SETUID))
  563. goto error;
  564. }
  565. if (new->uid != old->uid) {
  566. retval = set_user(new);
  567. if (retval < 0)
  568. goto error;
  569. }
  570. if (ruid != (uid_t) -1 ||
  571. (euid != (uid_t) -1 && euid != old->uid))
  572. new->suid = new->euid;
  573. new->fsuid = new->euid;
  574. retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
  575. if (retval < 0)
  576. goto error;
  577. return commit_creds(new);
  578. error:
  579. abort_creds(new);
  580. return retval;
  581. }
  582. /*
  583. * setuid() is implemented like SysV with SAVED_IDS
  584. *
  585. * Note that SAVED_ID's is deficient in that a setuid root program
  586. * like sendmail, for example, cannot set its uid to be a normal
  587. * user and then switch back, because if you're root, setuid() sets
  588. * the saved uid too. If you don't like this, blame the bright people
  589. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  590. * will allow a root program to temporarily drop privileges and be able to
  591. * regain them by swapping the real and effective uid.
  592. */
  593. SYSCALL_DEFINE1(setuid, uid_t, uid)
  594. {
  595. const struct cred *old;
  596. struct cred *new;
  597. int retval;
  598. new = prepare_creds();
  599. if (!new)
  600. return -ENOMEM;
  601. old = current_cred();
  602. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  603. if (retval)
  604. goto error;
  605. retval = -EPERM;
  606. if (capable(CAP_SETUID)) {
  607. new->suid = new->uid = uid;
  608. if (uid != old->uid) {
  609. retval = set_user(new);
  610. if (retval < 0)
  611. goto error;
  612. }
  613. } else if (uid != old->uid && uid != new->suid) {
  614. goto error;
  615. }
  616. new->fsuid = new->euid = uid;
  617. retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
  618. if (retval < 0)
  619. goto error;
  620. return commit_creds(new);
  621. error:
  622. abort_creds(new);
  623. return retval;
  624. }
  625. /*
  626. * This function implements a generic ability to update ruid, euid,
  627. * and suid. This allows you to implement the 4.4 compatible seteuid().
  628. */
  629. SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
  630. {
  631. const struct cred *old;
  632. struct cred *new;
  633. int retval;
  634. new = prepare_creds();
  635. if (!new)
  636. return -ENOMEM;
  637. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  638. if (retval)
  639. goto error;
  640. old = current_cred();
  641. retval = -EPERM;
  642. if (!capable(CAP_SETUID)) {
  643. if (ruid != (uid_t) -1 && ruid != old->uid &&
  644. ruid != old->euid && ruid != old->suid)
  645. goto error;
  646. if (euid != (uid_t) -1 && euid != old->uid &&
  647. euid != old->euid && euid != old->suid)
  648. goto error;
  649. if (suid != (uid_t) -1 && suid != old->uid &&
  650. suid != old->euid && suid != old->suid)
  651. goto error;
  652. }
  653. if (ruid != (uid_t) -1) {
  654. new->uid = ruid;
  655. if (ruid != old->uid) {
  656. retval = set_user(new);
  657. if (retval < 0)
  658. goto error;
  659. }
  660. }
  661. if (euid != (uid_t) -1)
  662. new->euid = euid;
  663. if (suid != (uid_t) -1)
  664. new->suid = suid;
  665. new->fsuid = new->euid;
  666. retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
  667. if (retval < 0)
  668. goto error;
  669. return commit_creds(new);
  670. error:
  671. abort_creds(new);
  672. return retval;
  673. }
  674. SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid)
  675. {
  676. const struct cred *cred = current_cred();
  677. int retval;
  678. if (!(retval = put_user(cred->uid, ruid)) &&
  679. !(retval = put_user(cred->euid, euid)))
  680. retval = put_user(cred->suid, suid);
  681. return retval;
  682. }
  683. /*
  684. * Same as above, but for rgid, egid, sgid.
  685. */
  686. SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
  687. {
  688. const struct cred *old;
  689. struct cred *new;
  690. int retval;
  691. new = prepare_creds();
  692. if (!new)
  693. return -ENOMEM;
  694. old = current_cred();
  695. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  696. if (retval)
  697. goto error;
  698. retval = -EPERM;
  699. if (!capable(CAP_SETGID)) {
  700. if (rgid != (gid_t) -1 && rgid != old->gid &&
  701. rgid != old->egid && rgid != old->sgid)
  702. goto error;
  703. if (egid != (gid_t) -1 && egid != old->gid &&
  704. egid != old->egid && egid != old->sgid)
  705. goto error;
  706. if (sgid != (gid_t) -1 && sgid != old->gid &&
  707. sgid != old->egid && sgid != old->sgid)
  708. goto error;
  709. }
  710. if (rgid != (gid_t) -1)
  711. new->gid = rgid;
  712. if (egid != (gid_t) -1)
  713. new->egid = egid;
  714. if (sgid != (gid_t) -1)
  715. new->sgid = sgid;
  716. new->fsgid = new->egid;
  717. return commit_creds(new);
  718. error:
  719. abort_creds(new);
  720. return retval;
  721. }
  722. SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid)
  723. {
  724. const struct cred *cred = current_cred();
  725. int retval;
  726. if (!(retval = put_user(cred->gid, rgid)) &&
  727. !(retval = put_user(cred->egid, egid)))
  728. retval = put_user(cred->sgid, sgid);
  729. return retval;
  730. }
  731. /*
  732. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  733. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  734. * whatever uid it wants to). It normally shadows "euid", except when
  735. * explicitly set by setfsuid() or for access..
  736. */
  737. SYSCALL_DEFINE1(setfsuid, uid_t, uid)
  738. {
  739. const struct cred *old;
  740. struct cred *new;
  741. uid_t old_fsuid;
  742. new = prepare_creds();
  743. if (!new)
  744. return current_fsuid();
  745. old = current_cred();
  746. old_fsuid = old->fsuid;
  747. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS) < 0)
  748. goto error;
  749. if (uid == old->uid || uid == old->euid ||
  750. uid == old->suid || uid == old->fsuid ||
  751. capable(CAP_SETUID)) {
  752. if (uid != old_fsuid) {
  753. new->fsuid = uid;
  754. if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
  755. goto change_okay;
  756. }
  757. }
  758. error:
  759. abort_creds(new);
  760. return old_fsuid;
  761. change_okay:
  762. commit_creds(new);
  763. return old_fsuid;
  764. }
  765. /*
  766. * Samma på svenska..
  767. */
  768. SYSCALL_DEFINE1(setfsgid, gid_t, gid)
  769. {
  770. const struct cred *old;
  771. struct cred *new;
  772. gid_t old_fsgid;
  773. new = prepare_creds();
  774. if (!new)
  775. return current_fsgid();
  776. old = current_cred();
  777. old_fsgid = old->fsgid;
  778. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  779. goto error;
  780. if (gid == old->gid || gid == old->egid ||
  781. gid == old->sgid || gid == old->fsgid ||
  782. capable(CAP_SETGID)) {
  783. if (gid != old_fsgid) {
  784. new->fsgid = gid;
  785. goto change_okay;
  786. }
  787. }
  788. error:
  789. abort_creds(new);
  790. return old_fsgid;
  791. change_okay:
  792. commit_creds(new);
  793. return old_fsgid;
  794. }
  795. void do_sys_times(struct tms *tms)
  796. {
  797. cputime_t tgutime, tgstime, cutime, cstime;
  798. spin_lock_irq(&current->sighand->siglock);
  799. thread_group_times(current, &tgutime, &tgstime);
  800. cutime = current->signal->cutime;
  801. cstime = current->signal->cstime;
  802. spin_unlock_irq(&current->sighand->siglock);
  803. tms->tms_utime = cputime_to_clock_t(tgutime);
  804. tms->tms_stime = cputime_to_clock_t(tgstime);
  805. tms->tms_cutime = cputime_to_clock_t(cutime);
  806. tms->tms_cstime = cputime_to_clock_t(cstime);
  807. }
  808. SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
  809. {
  810. if (tbuf) {
  811. struct tms tmp;
  812. do_sys_times(&tmp);
  813. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  814. return -EFAULT;
  815. }
  816. force_successful_syscall_return();
  817. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  818. }
  819. /*
  820. * This needs some heavy checking ...
  821. * I just haven't the stomach for it. I also don't fully
  822. * understand sessions/pgrp etc. Let somebody who does explain it.
  823. *
  824. * OK, I think I have the protection semantics right.... this is really
  825. * only important on a multi-user system anyway, to make sure one user
  826. * can't send a signal to a process owned by another. -TYT, 12/12/91
  827. *
  828. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  829. * LBT 04.03.94
  830. */
  831. SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
  832. {
  833. struct task_struct *p;
  834. struct task_struct *group_leader = current->group_leader;
  835. struct pid *pgrp;
  836. int err;
  837. if (!pid)
  838. pid = task_pid_vnr(group_leader);
  839. if (!pgid)
  840. pgid = pid;
  841. if (pgid < 0)
  842. return -EINVAL;
  843. /* From this point forward we keep holding onto the tasklist lock
  844. * so that our parent does not change from under us. -DaveM
  845. */
  846. write_lock_irq(&tasklist_lock);
  847. err = -ESRCH;
  848. p = find_task_by_vpid(pid);
  849. if (!p)
  850. goto out;
  851. err = -EINVAL;
  852. if (!thread_group_leader(p))
  853. goto out;
  854. if (same_thread_group(p->real_parent, group_leader)) {
  855. err = -EPERM;
  856. if (task_session(p) != task_session(group_leader))
  857. goto out;
  858. err = -EACCES;
  859. if (p->did_exec)
  860. goto out;
  861. } else {
  862. err = -ESRCH;
  863. if (p != group_leader)
  864. goto out;
  865. }
  866. err = -EPERM;
  867. if (p->signal->leader)
  868. goto out;
  869. pgrp = task_pid(p);
  870. if (pgid != pid) {
  871. struct task_struct *g;
  872. pgrp = find_vpid(pgid);
  873. g = pid_task(pgrp, PIDTYPE_PGID);
  874. if (!g || task_session(g) != task_session(group_leader))
  875. goto out;
  876. }
  877. err = security_task_setpgid(p, pgid);
  878. if (err)
  879. goto out;
  880. if (task_pgrp(p) != pgrp)
  881. change_pid(p, PIDTYPE_PGID, pgrp);
  882. err = 0;
  883. out:
  884. /* All paths lead to here, thus we are safe. -DaveM */
  885. write_unlock_irq(&tasklist_lock);
  886. return err;
  887. }
  888. SYSCALL_DEFINE1(getpgid, pid_t, pid)
  889. {
  890. struct task_struct *p;
  891. struct pid *grp;
  892. int retval;
  893. rcu_read_lock();
  894. if (!pid)
  895. grp = task_pgrp(current);
  896. else {
  897. retval = -ESRCH;
  898. p = find_task_by_vpid(pid);
  899. if (!p)
  900. goto out;
  901. grp = task_pgrp(p);
  902. if (!grp)
  903. goto out;
  904. retval = security_task_getpgid(p);
  905. if (retval)
  906. goto out;
  907. }
  908. retval = pid_vnr(grp);
  909. out:
  910. rcu_read_unlock();
  911. return retval;
  912. }
  913. #ifdef __ARCH_WANT_SYS_GETPGRP
  914. SYSCALL_DEFINE0(getpgrp)
  915. {
  916. return sys_getpgid(0);
  917. }
  918. #endif
  919. SYSCALL_DEFINE1(getsid, pid_t, pid)
  920. {
  921. struct task_struct *p;
  922. struct pid *sid;
  923. int retval;
  924. rcu_read_lock();
  925. if (!pid)
  926. sid = task_session(current);
  927. else {
  928. retval = -ESRCH;
  929. p = find_task_by_vpid(pid);
  930. if (!p)
  931. goto out;
  932. sid = task_session(p);
  933. if (!sid)
  934. goto out;
  935. retval = security_task_getsid(p);
  936. if (retval)
  937. goto out;
  938. }
  939. retval = pid_vnr(sid);
  940. out:
  941. rcu_read_unlock();
  942. return retval;
  943. }
  944. SYSCALL_DEFINE0(setsid)
  945. {
  946. struct task_struct *group_leader = current->group_leader;
  947. struct pid *sid = task_pid(group_leader);
  948. pid_t session = pid_vnr(sid);
  949. int err = -EPERM;
  950. write_lock_irq(&tasklist_lock);
  951. /* Fail if I am already a session leader */
  952. if (group_leader->signal->leader)
  953. goto out;
  954. /* Fail if a process group id already exists that equals the
  955. * proposed session id.
  956. */
  957. if (pid_task(sid, PIDTYPE_PGID))
  958. goto out;
  959. group_leader->signal->leader = 1;
  960. __set_special_pids(sid);
  961. proc_clear_tty(group_leader);
  962. err = session;
  963. out:
  964. write_unlock_irq(&tasklist_lock);
  965. if (err > 0)
  966. proc_sid_connector(group_leader);
  967. return err;
  968. }
  969. DECLARE_RWSEM(uts_sem);
  970. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  971. {
  972. int errno = 0;
  973. down_read(&uts_sem);
  974. if (copy_to_user(name, utsname(), sizeof *name))
  975. errno = -EFAULT;
  976. up_read(&uts_sem);
  977. return errno;
  978. }
  979. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  980. {
  981. int errno;
  982. char tmp[__NEW_UTS_LEN];
  983. if (!capable(CAP_SYS_ADMIN))
  984. return -EPERM;
  985. if (len < 0 || len > __NEW_UTS_LEN)
  986. return -EINVAL;
  987. down_write(&uts_sem);
  988. errno = -EFAULT;
  989. if (!copy_from_user(tmp, name, len)) {
  990. struct new_utsname *u = utsname();
  991. memcpy(u->nodename, tmp, len);
  992. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  993. errno = 0;
  994. }
  995. up_write(&uts_sem);
  996. return errno;
  997. }
  998. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  999. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1000. {
  1001. int i, errno;
  1002. struct new_utsname *u;
  1003. if (len < 0)
  1004. return -EINVAL;
  1005. down_read(&uts_sem);
  1006. u = utsname();
  1007. i = 1 + strlen(u->nodename);
  1008. if (i > len)
  1009. i = len;
  1010. errno = 0;
  1011. if (copy_to_user(name, u->nodename, i))
  1012. errno = -EFAULT;
  1013. up_read(&uts_sem);
  1014. return errno;
  1015. }
  1016. #endif
  1017. /*
  1018. * Only setdomainname; getdomainname can be implemented by calling
  1019. * uname()
  1020. */
  1021. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1022. {
  1023. int errno;
  1024. char tmp[__NEW_UTS_LEN];
  1025. if (!capable(CAP_SYS_ADMIN))
  1026. return -EPERM;
  1027. if (len < 0 || len > __NEW_UTS_LEN)
  1028. return -EINVAL;
  1029. down_write(&uts_sem);
  1030. errno = -EFAULT;
  1031. if (!copy_from_user(tmp, name, len)) {
  1032. struct new_utsname *u = utsname();
  1033. memcpy(u->domainname, tmp, len);
  1034. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1035. errno = 0;
  1036. }
  1037. up_write(&uts_sem);
  1038. return errno;
  1039. }
  1040. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1041. {
  1042. if (resource >= RLIM_NLIMITS)
  1043. return -EINVAL;
  1044. else {
  1045. struct rlimit value;
  1046. task_lock(current->group_leader);
  1047. value = current->signal->rlim[resource];
  1048. task_unlock(current->group_leader);
  1049. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1050. }
  1051. }
  1052. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1053. /*
  1054. * Back compatibility for getrlimit. Needed for some apps.
  1055. */
  1056. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1057. struct rlimit __user *, rlim)
  1058. {
  1059. struct rlimit x;
  1060. if (resource >= RLIM_NLIMITS)
  1061. return -EINVAL;
  1062. task_lock(current->group_leader);
  1063. x = current->signal->rlim[resource];
  1064. task_unlock(current->group_leader);
  1065. if (x.rlim_cur > 0x7FFFFFFF)
  1066. x.rlim_cur = 0x7FFFFFFF;
  1067. if (x.rlim_max > 0x7FFFFFFF)
  1068. x.rlim_max = 0x7FFFFFFF;
  1069. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1070. }
  1071. #endif
  1072. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1073. {
  1074. struct rlimit new_rlim, *old_rlim;
  1075. int retval;
  1076. if (resource >= RLIM_NLIMITS)
  1077. return -EINVAL;
  1078. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1079. return -EFAULT;
  1080. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1081. return -EINVAL;
  1082. old_rlim = current->signal->rlim + resource;
  1083. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1084. !capable(CAP_SYS_RESOURCE))
  1085. return -EPERM;
  1086. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > sysctl_nr_open)
  1087. return -EPERM;
  1088. retval = security_task_setrlimit(resource, &new_rlim);
  1089. if (retval)
  1090. return retval;
  1091. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1092. /*
  1093. * The caller is asking for an immediate RLIMIT_CPU
  1094. * expiry. But we use the zero value to mean "it was
  1095. * never set". So let's cheat and make it one second
  1096. * instead
  1097. */
  1098. new_rlim.rlim_cur = 1;
  1099. }
  1100. task_lock(current->group_leader);
  1101. *old_rlim = new_rlim;
  1102. task_unlock(current->group_leader);
  1103. if (resource != RLIMIT_CPU)
  1104. goto out;
  1105. /*
  1106. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1107. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1108. * very long-standing error, and fixing it now risks breakage of
  1109. * applications, so we live with it
  1110. */
  1111. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1112. goto out;
  1113. update_rlimit_cpu(new_rlim.rlim_cur);
  1114. out:
  1115. return 0;
  1116. }
  1117. /*
  1118. * It would make sense to put struct rusage in the task_struct,
  1119. * except that would make the task_struct be *really big*. After
  1120. * task_struct gets moved into malloc'ed memory, it would
  1121. * make sense to do this. It will make moving the rest of the information
  1122. * a lot simpler! (Which we're not doing right now because we're not
  1123. * measuring them yet).
  1124. *
  1125. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1126. * races with threads incrementing their own counters. But since word
  1127. * reads are atomic, we either get new values or old values and we don't
  1128. * care which for the sums. We always take the siglock to protect reading
  1129. * the c* fields from p->signal from races with exit.c updating those
  1130. * fields when reaping, so a sample either gets all the additions of a
  1131. * given child after it's reaped, or none so this sample is before reaping.
  1132. *
  1133. * Locking:
  1134. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1135. * for the cases current multithreaded, non-current single threaded
  1136. * non-current multithreaded. Thread traversal is now safe with
  1137. * the siglock held.
  1138. * Strictly speaking, we donot need to take the siglock if we are current and
  1139. * single threaded, as no one else can take our signal_struct away, no one
  1140. * else can reap the children to update signal->c* counters, and no one else
  1141. * can race with the signal-> fields. If we do not take any lock, the
  1142. * signal-> fields could be read out of order while another thread was just
  1143. * exiting. So we should place a read memory barrier when we avoid the lock.
  1144. * On the writer side, write memory barrier is implied in __exit_signal
  1145. * as __exit_signal releases the siglock spinlock after updating the signal->
  1146. * fields. But we don't do this yet to keep things simple.
  1147. *
  1148. */
  1149. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1150. {
  1151. r->ru_nvcsw += t->nvcsw;
  1152. r->ru_nivcsw += t->nivcsw;
  1153. r->ru_minflt += t->min_flt;
  1154. r->ru_majflt += t->maj_flt;
  1155. r->ru_inblock += task_io_get_inblock(t);
  1156. r->ru_oublock += task_io_get_oublock(t);
  1157. }
  1158. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1159. {
  1160. struct task_struct *t;
  1161. unsigned long flags;
  1162. cputime_t tgutime, tgstime, utime, stime;
  1163. unsigned long maxrss = 0;
  1164. memset((char *) r, 0, sizeof *r);
  1165. utime = stime = cputime_zero;
  1166. if (who == RUSAGE_THREAD) {
  1167. task_times(current, &utime, &stime);
  1168. accumulate_thread_rusage(p, r);
  1169. maxrss = p->signal->maxrss;
  1170. goto out;
  1171. }
  1172. if (!lock_task_sighand(p, &flags))
  1173. return;
  1174. switch (who) {
  1175. case RUSAGE_BOTH:
  1176. case RUSAGE_CHILDREN:
  1177. utime = p->signal->cutime;
  1178. stime = p->signal->cstime;
  1179. r->ru_nvcsw = p->signal->cnvcsw;
  1180. r->ru_nivcsw = p->signal->cnivcsw;
  1181. r->ru_minflt = p->signal->cmin_flt;
  1182. r->ru_majflt = p->signal->cmaj_flt;
  1183. r->ru_inblock = p->signal->cinblock;
  1184. r->ru_oublock = p->signal->coublock;
  1185. maxrss = p->signal->cmaxrss;
  1186. if (who == RUSAGE_CHILDREN)
  1187. break;
  1188. case RUSAGE_SELF:
  1189. thread_group_times(p, &tgutime, &tgstime);
  1190. utime = cputime_add(utime, tgutime);
  1191. stime = cputime_add(stime, tgstime);
  1192. r->ru_nvcsw += p->signal->nvcsw;
  1193. r->ru_nivcsw += p->signal->nivcsw;
  1194. r->ru_minflt += p->signal->min_flt;
  1195. r->ru_majflt += p->signal->maj_flt;
  1196. r->ru_inblock += p->signal->inblock;
  1197. r->ru_oublock += p->signal->oublock;
  1198. if (maxrss < p->signal->maxrss)
  1199. maxrss = p->signal->maxrss;
  1200. t = p;
  1201. do {
  1202. accumulate_thread_rusage(t, r);
  1203. t = next_thread(t);
  1204. } while (t != p);
  1205. break;
  1206. default:
  1207. BUG();
  1208. }
  1209. unlock_task_sighand(p, &flags);
  1210. out:
  1211. cputime_to_timeval(utime, &r->ru_utime);
  1212. cputime_to_timeval(stime, &r->ru_stime);
  1213. if (who != RUSAGE_CHILDREN) {
  1214. struct mm_struct *mm = get_task_mm(p);
  1215. if (mm) {
  1216. setmax_mm_hiwater_rss(&maxrss, mm);
  1217. mmput(mm);
  1218. }
  1219. }
  1220. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1221. }
  1222. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1223. {
  1224. struct rusage r;
  1225. k_getrusage(p, who, &r);
  1226. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1227. }
  1228. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1229. {
  1230. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1231. who != RUSAGE_THREAD)
  1232. return -EINVAL;
  1233. return getrusage(current, who, ru);
  1234. }
  1235. SYSCALL_DEFINE1(umask, int, mask)
  1236. {
  1237. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1238. return mask;
  1239. }
  1240. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1241. unsigned long, arg4, unsigned long, arg5)
  1242. {
  1243. struct task_struct *me = current;
  1244. unsigned char comm[sizeof(me->comm)];
  1245. long error;
  1246. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1247. if (error != -ENOSYS)
  1248. return error;
  1249. error = 0;
  1250. switch (option) {
  1251. case PR_SET_PDEATHSIG:
  1252. if (!valid_signal(arg2)) {
  1253. error = -EINVAL;
  1254. break;
  1255. }
  1256. me->pdeath_signal = arg2;
  1257. error = 0;
  1258. break;
  1259. case PR_GET_PDEATHSIG:
  1260. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1261. break;
  1262. case PR_GET_DUMPABLE:
  1263. error = get_dumpable(me->mm);
  1264. break;
  1265. case PR_SET_DUMPABLE:
  1266. if (arg2 < 0 || arg2 > 1) {
  1267. error = -EINVAL;
  1268. break;
  1269. }
  1270. set_dumpable(me->mm, arg2);
  1271. error = 0;
  1272. break;
  1273. case PR_SET_UNALIGN:
  1274. error = SET_UNALIGN_CTL(me, arg2);
  1275. break;
  1276. case PR_GET_UNALIGN:
  1277. error = GET_UNALIGN_CTL(me, arg2);
  1278. break;
  1279. case PR_SET_FPEMU:
  1280. error = SET_FPEMU_CTL(me, arg2);
  1281. break;
  1282. case PR_GET_FPEMU:
  1283. error = GET_FPEMU_CTL(me, arg2);
  1284. break;
  1285. case PR_SET_FPEXC:
  1286. error = SET_FPEXC_CTL(me, arg2);
  1287. break;
  1288. case PR_GET_FPEXC:
  1289. error = GET_FPEXC_CTL(me, arg2);
  1290. break;
  1291. case PR_GET_TIMING:
  1292. error = PR_TIMING_STATISTICAL;
  1293. break;
  1294. case PR_SET_TIMING:
  1295. if (arg2 != PR_TIMING_STATISTICAL)
  1296. error = -EINVAL;
  1297. else
  1298. error = 0;
  1299. break;
  1300. case PR_SET_NAME:
  1301. comm[sizeof(me->comm)-1] = 0;
  1302. if (strncpy_from_user(comm, (char __user *)arg2,
  1303. sizeof(me->comm) - 1) < 0)
  1304. return -EFAULT;
  1305. set_task_comm(me, comm);
  1306. return 0;
  1307. case PR_GET_NAME:
  1308. get_task_comm(comm, me);
  1309. if (copy_to_user((char __user *)arg2, comm,
  1310. sizeof(comm)))
  1311. return -EFAULT;
  1312. return 0;
  1313. case PR_GET_ENDIAN:
  1314. error = GET_ENDIAN(me, arg2);
  1315. break;
  1316. case PR_SET_ENDIAN:
  1317. error = SET_ENDIAN(me, arg2);
  1318. break;
  1319. case PR_GET_SECCOMP:
  1320. error = prctl_get_seccomp();
  1321. break;
  1322. case PR_SET_SECCOMP:
  1323. error = prctl_set_seccomp(arg2);
  1324. break;
  1325. case PR_GET_TSC:
  1326. error = GET_TSC_CTL(arg2);
  1327. break;
  1328. case PR_SET_TSC:
  1329. error = SET_TSC_CTL(arg2);
  1330. break;
  1331. case PR_TASK_PERF_EVENTS_DISABLE:
  1332. error = perf_event_task_disable();
  1333. break;
  1334. case PR_TASK_PERF_EVENTS_ENABLE:
  1335. error = perf_event_task_enable();
  1336. break;
  1337. case PR_GET_TIMERSLACK:
  1338. error = current->timer_slack_ns;
  1339. break;
  1340. case PR_SET_TIMERSLACK:
  1341. if (arg2 <= 0)
  1342. current->timer_slack_ns =
  1343. current->default_timer_slack_ns;
  1344. else
  1345. current->timer_slack_ns = arg2;
  1346. error = 0;
  1347. break;
  1348. case PR_MCE_KILL:
  1349. if (arg4 | arg5)
  1350. return -EINVAL;
  1351. switch (arg2) {
  1352. case PR_MCE_KILL_CLEAR:
  1353. if (arg3 != 0)
  1354. return -EINVAL;
  1355. current->flags &= ~PF_MCE_PROCESS;
  1356. break;
  1357. case PR_MCE_KILL_SET:
  1358. current->flags |= PF_MCE_PROCESS;
  1359. if (arg3 == PR_MCE_KILL_EARLY)
  1360. current->flags |= PF_MCE_EARLY;
  1361. else if (arg3 == PR_MCE_KILL_LATE)
  1362. current->flags &= ~PF_MCE_EARLY;
  1363. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1364. current->flags &=
  1365. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1366. else
  1367. return -EINVAL;
  1368. break;
  1369. default:
  1370. return -EINVAL;
  1371. }
  1372. error = 0;
  1373. break;
  1374. case PR_MCE_KILL_GET:
  1375. if (arg2 | arg3 | arg4 | arg5)
  1376. return -EINVAL;
  1377. if (current->flags & PF_MCE_PROCESS)
  1378. error = (current->flags & PF_MCE_EARLY) ?
  1379. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1380. else
  1381. error = PR_MCE_KILL_DEFAULT;
  1382. break;
  1383. default:
  1384. error = -EINVAL;
  1385. break;
  1386. }
  1387. return error;
  1388. }
  1389. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1390. struct getcpu_cache __user *, unused)
  1391. {
  1392. int err = 0;
  1393. int cpu = raw_smp_processor_id();
  1394. if (cpup)
  1395. err |= put_user(cpu, cpup);
  1396. if (nodep)
  1397. err |= put_user(cpu_to_node(cpu), nodep);
  1398. return err ? -EFAULT : 0;
  1399. }
  1400. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1401. static void argv_cleanup(char **argv, char **envp)
  1402. {
  1403. argv_free(argv);
  1404. }
  1405. /**
  1406. * orderly_poweroff - Trigger an orderly system poweroff
  1407. * @force: force poweroff if command execution fails
  1408. *
  1409. * This may be called from any context to trigger a system shutdown.
  1410. * If the orderly shutdown fails, it will force an immediate shutdown.
  1411. */
  1412. int orderly_poweroff(bool force)
  1413. {
  1414. int argc;
  1415. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  1416. static char *envp[] = {
  1417. "HOME=/",
  1418. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  1419. NULL
  1420. };
  1421. int ret = -ENOMEM;
  1422. struct subprocess_info *info;
  1423. if (argv == NULL) {
  1424. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  1425. __func__, poweroff_cmd);
  1426. goto out;
  1427. }
  1428. info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC);
  1429. if (info == NULL) {
  1430. argv_free(argv);
  1431. goto out;
  1432. }
  1433. call_usermodehelper_setcleanup(info, argv_cleanup);
  1434. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  1435. out:
  1436. if (ret && force) {
  1437. printk(KERN_WARNING "Failed to start orderly shutdown: "
  1438. "forcing the issue\n");
  1439. /* I guess this should try to kick off some daemon to
  1440. sync and poweroff asap. Or not even bother syncing
  1441. if we're doing an emergency shutdown? */
  1442. emergency_sync();
  1443. kernel_power_off();
  1444. }
  1445. return ret;
  1446. }
  1447. EXPORT_SYMBOL_GPL(orderly_poweroff);