sys.c 52 KB

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