sys.c 44 KB

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