sys.c 58 KB

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