sys.c 42 KB

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