sys.c 41 KB

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