sys.c 58 KB

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