sys.c 60 KB

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