sys.c 47 KB

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