sys.c 43 KB

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