sys.c 47 KB

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