sys.c 61 KB

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