sys.c 47 KB

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