sys.c 41 KB

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