sys.c 60 KB

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