sys.c 61 KB

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