sys.c 47 KB

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