sys.c 62 KB

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