sys.c 50 KB

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