sys.c 42 KB

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