sys.c 62 KB

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