sys.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499
  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.\n");
  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. static void set_special_pids(struct pid *pid)
  1135. {
  1136. struct task_struct *curr = current->group_leader;
  1137. if (task_session(curr) != pid)
  1138. change_pid(curr, PIDTYPE_SID, pid);
  1139. if (task_pgrp(curr) != pid)
  1140. change_pid(curr, PIDTYPE_PGID, pid);
  1141. }
  1142. SYSCALL_DEFINE0(setsid)
  1143. {
  1144. struct task_struct *group_leader = current->group_leader;
  1145. struct pid *sid = task_pid(group_leader);
  1146. pid_t session = pid_vnr(sid);
  1147. int err = -EPERM;
  1148. write_lock_irq(&tasklist_lock);
  1149. /* Fail if I am already a session leader */
  1150. if (group_leader->signal->leader)
  1151. goto out;
  1152. /* Fail if a process group id already exists that equals the
  1153. * proposed session id.
  1154. */
  1155. if (pid_task(sid, PIDTYPE_PGID))
  1156. goto out;
  1157. group_leader->signal->leader = 1;
  1158. set_special_pids(sid);
  1159. proc_clear_tty(group_leader);
  1160. err = session;
  1161. out:
  1162. write_unlock_irq(&tasklist_lock);
  1163. if (err > 0) {
  1164. proc_sid_connector(group_leader);
  1165. sched_autogroup_create_attach(group_leader);
  1166. }
  1167. return err;
  1168. }
  1169. DECLARE_RWSEM(uts_sem);
  1170. #ifdef COMPAT_UTS_MACHINE
  1171. #define override_architecture(name) \
  1172. (personality(current->personality) == PER_LINUX32 && \
  1173. copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
  1174. sizeof(COMPAT_UTS_MACHINE)))
  1175. #else
  1176. #define override_architecture(name) 0
  1177. #endif
  1178. /*
  1179. * Work around broken programs that cannot handle "Linux 3.0".
  1180. * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
  1181. */
  1182. static int override_release(char __user *release, size_t len)
  1183. {
  1184. int ret = 0;
  1185. if (current->personality & UNAME26) {
  1186. const char *rest = UTS_RELEASE;
  1187. char buf[65] = { 0 };
  1188. int ndots = 0;
  1189. unsigned v;
  1190. size_t copy;
  1191. while (*rest) {
  1192. if (*rest == '.' && ++ndots >= 3)
  1193. break;
  1194. if (!isdigit(*rest) && *rest != '.')
  1195. break;
  1196. rest++;
  1197. }
  1198. v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40;
  1199. copy = clamp_t(size_t, len, 1, sizeof(buf));
  1200. copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
  1201. ret = copy_to_user(release, buf, copy + 1);
  1202. }
  1203. return ret;
  1204. }
  1205. SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
  1206. {
  1207. int errno = 0;
  1208. down_read(&uts_sem);
  1209. if (copy_to_user(name, utsname(), sizeof *name))
  1210. errno = -EFAULT;
  1211. up_read(&uts_sem);
  1212. if (!errno && override_release(name->release, sizeof(name->release)))
  1213. errno = -EFAULT;
  1214. if (!errno && override_architecture(name))
  1215. errno = -EFAULT;
  1216. return errno;
  1217. }
  1218. #ifdef __ARCH_WANT_SYS_OLD_UNAME
  1219. /*
  1220. * Old cruft
  1221. */
  1222. SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
  1223. {
  1224. int error = 0;
  1225. if (!name)
  1226. return -EFAULT;
  1227. down_read(&uts_sem);
  1228. if (copy_to_user(name, utsname(), sizeof(*name)))
  1229. error = -EFAULT;
  1230. up_read(&uts_sem);
  1231. if (!error && override_release(name->release, sizeof(name->release)))
  1232. error = -EFAULT;
  1233. if (!error && override_architecture(name))
  1234. error = -EFAULT;
  1235. return error;
  1236. }
  1237. SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
  1238. {
  1239. int error;
  1240. if (!name)
  1241. return -EFAULT;
  1242. if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname)))
  1243. return -EFAULT;
  1244. down_read(&uts_sem);
  1245. error = __copy_to_user(&name->sysname, &utsname()->sysname,
  1246. __OLD_UTS_LEN);
  1247. error |= __put_user(0, name->sysname + __OLD_UTS_LEN);
  1248. error |= __copy_to_user(&name->nodename, &utsname()->nodename,
  1249. __OLD_UTS_LEN);
  1250. error |= __put_user(0, name->nodename + __OLD_UTS_LEN);
  1251. error |= __copy_to_user(&name->release, &utsname()->release,
  1252. __OLD_UTS_LEN);
  1253. error |= __put_user(0, name->release + __OLD_UTS_LEN);
  1254. error |= __copy_to_user(&name->version, &utsname()->version,
  1255. __OLD_UTS_LEN);
  1256. error |= __put_user(0, name->version + __OLD_UTS_LEN);
  1257. error |= __copy_to_user(&name->machine, &utsname()->machine,
  1258. __OLD_UTS_LEN);
  1259. error |= __put_user(0, name->machine + __OLD_UTS_LEN);
  1260. up_read(&uts_sem);
  1261. if (!error && override_architecture(name))
  1262. error = -EFAULT;
  1263. if (!error && override_release(name->release, sizeof(name->release)))
  1264. error = -EFAULT;
  1265. return error ? -EFAULT : 0;
  1266. }
  1267. #endif
  1268. SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
  1269. {
  1270. int errno;
  1271. char tmp[__NEW_UTS_LEN];
  1272. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1273. return -EPERM;
  1274. if (len < 0 || len > __NEW_UTS_LEN)
  1275. return -EINVAL;
  1276. down_write(&uts_sem);
  1277. errno = -EFAULT;
  1278. if (!copy_from_user(tmp, name, len)) {
  1279. struct new_utsname *u = utsname();
  1280. memcpy(u->nodename, tmp, len);
  1281. memset(u->nodename + len, 0, sizeof(u->nodename) - len);
  1282. errno = 0;
  1283. uts_proc_notify(UTS_PROC_HOSTNAME);
  1284. }
  1285. up_write(&uts_sem);
  1286. return errno;
  1287. }
  1288. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1289. SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
  1290. {
  1291. int i, errno;
  1292. struct new_utsname *u;
  1293. if (len < 0)
  1294. return -EINVAL;
  1295. down_read(&uts_sem);
  1296. u = utsname();
  1297. i = 1 + strlen(u->nodename);
  1298. if (i > len)
  1299. i = len;
  1300. errno = 0;
  1301. if (copy_to_user(name, u->nodename, i))
  1302. errno = -EFAULT;
  1303. up_read(&uts_sem);
  1304. return errno;
  1305. }
  1306. #endif
  1307. /*
  1308. * Only setdomainname; getdomainname can be implemented by calling
  1309. * uname()
  1310. */
  1311. SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
  1312. {
  1313. int errno;
  1314. char tmp[__NEW_UTS_LEN];
  1315. if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
  1316. return -EPERM;
  1317. if (len < 0 || len > __NEW_UTS_LEN)
  1318. return -EINVAL;
  1319. down_write(&uts_sem);
  1320. errno = -EFAULT;
  1321. if (!copy_from_user(tmp, name, len)) {
  1322. struct new_utsname *u = utsname();
  1323. memcpy(u->domainname, tmp, len);
  1324. memset(u->domainname + len, 0, sizeof(u->domainname) - len);
  1325. errno = 0;
  1326. uts_proc_notify(UTS_PROC_DOMAINNAME);
  1327. }
  1328. up_write(&uts_sem);
  1329. return errno;
  1330. }
  1331. SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1332. {
  1333. struct rlimit value;
  1334. int ret;
  1335. ret = do_prlimit(current, resource, NULL, &value);
  1336. if (!ret)
  1337. ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1338. return ret;
  1339. }
  1340. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1341. /*
  1342. * Back compatibility for getrlimit. Needed for some apps.
  1343. */
  1344. SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
  1345. struct rlimit __user *, rlim)
  1346. {
  1347. struct rlimit x;
  1348. if (resource >= RLIM_NLIMITS)
  1349. return -EINVAL;
  1350. task_lock(current->group_leader);
  1351. x = current->signal->rlim[resource];
  1352. task_unlock(current->group_leader);
  1353. if (x.rlim_cur > 0x7FFFFFFF)
  1354. x.rlim_cur = 0x7FFFFFFF;
  1355. if (x.rlim_max > 0x7FFFFFFF)
  1356. x.rlim_max = 0x7FFFFFFF;
  1357. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1358. }
  1359. #endif
  1360. static inline bool rlim64_is_infinity(__u64 rlim64)
  1361. {
  1362. #if BITS_PER_LONG < 64
  1363. return rlim64 >= ULONG_MAX;
  1364. #else
  1365. return rlim64 == RLIM64_INFINITY;
  1366. #endif
  1367. }
  1368. static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
  1369. {
  1370. if (rlim->rlim_cur == RLIM_INFINITY)
  1371. rlim64->rlim_cur = RLIM64_INFINITY;
  1372. else
  1373. rlim64->rlim_cur = rlim->rlim_cur;
  1374. if (rlim->rlim_max == RLIM_INFINITY)
  1375. rlim64->rlim_max = RLIM64_INFINITY;
  1376. else
  1377. rlim64->rlim_max = rlim->rlim_max;
  1378. }
  1379. static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
  1380. {
  1381. if (rlim64_is_infinity(rlim64->rlim_cur))
  1382. rlim->rlim_cur = RLIM_INFINITY;
  1383. else
  1384. rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
  1385. if (rlim64_is_infinity(rlim64->rlim_max))
  1386. rlim->rlim_max = RLIM_INFINITY;
  1387. else
  1388. rlim->rlim_max = (unsigned long)rlim64->rlim_max;
  1389. }
  1390. /* make sure you are allowed to change @tsk limits before calling this */
  1391. int do_prlimit(struct task_struct *tsk, unsigned int resource,
  1392. struct rlimit *new_rlim, struct rlimit *old_rlim)
  1393. {
  1394. struct rlimit *rlim;
  1395. int retval = 0;
  1396. if (resource >= RLIM_NLIMITS)
  1397. return -EINVAL;
  1398. if (new_rlim) {
  1399. if (new_rlim->rlim_cur > new_rlim->rlim_max)
  1400. return -EINVAL;
  1401. if (resource == RLIMIT_NOFILE &&
  1402. new_rlim->rlim_max > sysctl_nr_open)
  1403. return -EPERM;
  1404. }
  1405. /* protect tsk->signal and tsk->sighand from disappearing */
  1406. read_lock(&tasklist_lock);
  1407. if (!tsk->sighand) {
  1408. retval = -ESRCH;
  1409. goto out;
  1410. }
  1411. rlim = tsk->signal->rlim + resource;
  1412. task_lock(tsk->group_leader);
  1413. if (new_rlim) {
  1414. /* Keep the capable check against init_user_ns until
  1415. cgroups can contain all limits */
  1416. if (new_rlim->rlim_max > rlim->rlim_max &&
  1417. !capable(CAP_SYS_RESOURCE))
  1418. retval = -EPERM;
  1419. if (!retval)
  1420. retval = security_task_setrlimit(tsk->group_leader,
  1421. resource, new_rlim);
  1422. if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
  1423. /*
  1424. * The caller is asking for an immediate RLIMIT_CPU
  1425. * expiry. But we use the zero value to mean "it was
  1426. * never set". So let's cheat and make it one second
  1427. * instead
  1428. */
  1429. new_rlim->rlim_cur = 1;
  1430. }
  1431. }
  1432. if (!retval) {
  1433. if (old_rlim)
  1434. *old_rlim = *rlim;
  1435. if (new_rlim)
  1436. *rlim = *new_rlim;
  1437. }
  1438. task_unlock(tsk->group_leader);
  1439. /*
  1440. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1441. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1442. * very long-standing error, and fixing it now risks breakage of
  1443. * applications, so we live with it
  1444. */
  1445. if (!retval && new_rlim && resource == RLIMIT_CPU &&
  1446. new_rlim->rlim_cur != RLIM_INFINITY)
  1447. update_rlimit_cpu(tsk, new_rlim->rlim_cur);
  1448. out:
  1449. read_unlock(&tasklist_lock);
  1450. return retval;
  1451. }
  1452. /* rcu lock must be held */
  1453. static int check_prlimit_permission(struct task_struct *task)
  1454. {
  1455. const struct cred *cred = current_cred(), *tcred;
  1456. if (current == task)
  1457. return 0;
  1458. tcred = __task_cred(task);
  1459. if (uid_eq(cred->uid, tcred->euid) &&
  1460. uid_eq(cred->uid, tcred->suid) &&
  1461. uid_eq(cred->uid, tcred->uid) &&
  1462. gid_eq(cred->gid, tcred->egid) &&
  1463. gid_eq(cred->gid, tcred->sgid) &&
  1464. gid_eq(cred->gid, tcred->gid))
  1465. return 0;
  1466. if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
  1467. return 0;
  1468. return -EPERM;
  1469. }
  1470. SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
  1471. const struct rlimit64 __user *, new_rlim,
  1472. struct rlimit64 __user *, old_rlim)
  1473. {
  1474. struct rlimit64 old64, new64;
  1475. struct rlimit old, new;
  1476. struct task_struct *tsk;
  1477. int ret;
  1478. if (new_rlim) {
  1479. if (copy_from_user(&new64, new_rlim, sizeof(new64)))
  1480. return -EFAULT;
  1481. rlim64_to_rlim(&new64, &new);
  1482. }
  1483. rcu_read_lock();
  1484. tsk = pid ? find_task_by_vpid(pid) : current;
  1485. if (!tsk) {
  1486. rcu_read_unlock();
  1487. return -ESRCH;
  1488. }
  1489. ret = check_prlimit_permission(tsk);
  1490. if (ret) {
  1491. rcu_read_unlock();
  1492. return ret;
  1493. }
  1494. get_task_struct(tsk);
  1495. rcu_read_unlock();
  1496. ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
  1497. old_rlim ? &old : NULL);
  1498. if (!ret && old_rlim) {
  1499. rlim_to_rlim64(&old, &old64);
  1500. if (copy_to_user(old_rlim, &old64, sizeof(old64)))
  1501. ret = -EFAULT;
  1502. }
  1503. put_task_struct(tsk);
  1504. return ret;
  1505. }
  1506. SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
  1507. {
  1508. struct rlimit new_rlim;
  1509. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1510. return -EFAULT;
  1511. return do_prlimit(current, resource, &new_rlim, NULL);
  1512. }
  1513. /*
  1514. * It would make sense to put struct rusage in the task_struct,
  1515. * except that would make the task_struct be *really big*. After
  1516. * task_struct gets moved into malloc'ed memory, it would
  1517. * make sense to do this. It will make moving the rest of the information
  1518. * a lot simpler! (Which we're not doing right now because we're not
  1519. * measuring them yet).
  1520. *
  1521. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1522. * races with threads incrementing their own counters. But since word
  1523. * reads are atomic, we either get new values or old values and we don't
  1524. * care which for the sums. We always take the siglock to protect reading
  1525. * the c* fields from p->signal from races with exit.c updating those
  1526. * fields when reaping, so a sample either gets all the additions of a
  1527. * given child after it's reaped, or none so this sample is before reaping.
  1528. *
  1529. * Locking:
  1530. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1531. * for the cases current multithreaded, non-current single threaded
  1532. * non-current multithreaded. Thread traversal is now safe with
  1533. * the siglock held.
  1534. * Strictly speaking, we donot need to take the siglock if we are current and
  1535. * single threaded, as no one else can take our signal_struct away, no one
  1536. * else can reap the children to update signal->c* counters, and no one else
  1537. * can race with the signal-> fields. If we do not take any lock, the
  1538. * signal-> fields could be read out of order while another thread was just
  1539. * exiting. So we should place a read memory barrier when we avoid the lock.
  1540. * On the writer side, write memory barrier is implied in __exit_signal
  1541. * as __exit_signal releases the siglock spinlock after updating the signal->
  1542. * fields. But we don't do this yet to keep things simple.
  1543. *
  1544. */
  1545. static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
  1546. {
  1547. r->ru_nvcsw += t->nvcsw;
  1548. r->ru_nivcsw += t->nivcsw;
  1549. r->ru_minflt += t->min_flt;
  1550. r->ru_majflt += t->maj_flt;
  1551. r->ru_inblock += task_io_get_inblock(t);
  1552. r->ru_oublock += task_io_get_oublock(t);
  1553. }
  1554. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1555. {
  1556. struct task_struct *t;
  1557. unsigned long flags;
  1558. cputime_t tgutime, tgstime, utime, stime;
  1559. unsigned long maxrss = 0;
  1560. memset((char *) r, 0, sizeof *r);
  1561. utime = stime = 0;
  1562. if (who == RUSAGE_THREAD) {
  1563. task_cputime_adjusted(current, &utime, &stime);
  1564. accumulate_thread_rusage(p, r);
  1565. maxrss = p->signal->maxrss;
  1566. goto out;
  1567. }
  1568. if (!lock_task_sighand(p, &flags))
  1569. return;
  1570. switch (who) {
  1571. case RUSAGE_BOTH:
  1572. case RUSAGE_CHILDREN:
  1573. utime = p->signal->cutime;
  1574. stime = p->signal->cstime;
  1575. r->ru_nvcsw = p->signal->cnvcsw;
  1576. r->ru_nivcsw = p->signal->cnivcsw;
  1577. r->ru_minflt = p->signal->cmin_flt;
  1578. r->ru_majflt = p->signal->cmaj_flt;
  1579. r->ru_inblock = p->signal->cinblock;
  1580. r->ru_oublock = p->signal->coublock;
  1581. maxrss = p->signal->cmaxrss;
  1582. if (who == RUSAGE_CHILDREN)
  1583. break;
  1584. case RUSAGE_SELF:
  1585. thread_group_cputime_adjusted(p, &tgutime, &tgstime);
  1586. utime += tgutime;
  1587. stime += tgstime;
  1588. r->ru_nvcsw += p->signal->nvcsw;
  1589. r->ru_nivcsw += p->signal->nivcsw;
  1590. r->ru_minflt += p->signal->min_flt;
  1591. r->ru_majflt += p->signal->maj_flt;
  1592. r->ru_inblock += p->signal->inblock;
  1593. r->ru_oublock += p->signal->oublock;
  1594. if (maxrss < p->signal->maxrss)
  1595. maxrss = p->signal->maxrss;
  1596. t = p;
  1597. do {
  1598. accumulate_thread_rusage(t, r);
  1599. t = next_thread(t);
  1600. } while (t != p);
  1601. break;
  1602. default:
  1603. BUG();
  1604. }
  1605. unlock_task_sighand(p, &flags);
  1606. out:
  1607. cputime_to_timeval(utime, &r->ru_utime);
  1608. cputime_to_timeval(stime, &r->ru_stime);
  1609. if (who != RUSAGE_CHILDREN) {
  1610. struct mm_struct *mm = get_task_mm(p);
  1611. if (mm) {
  1612. setmax_mm_hiwater_rss(&maxrss, mm);
  1613. mmput(mm);
  1614. }
  1615. }
  1616. r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
  1617. }
  1618. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1619. {
  1620. struct rusage r;
  1621. k_getrusage(p, who, &r);
  1622. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1623. }
  1624. SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
  1625. {
  1626. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1627. who != RUSAGE_THREAD)
  1628. return -EINVAL;
  1629. return getrusage(current, who, ru);
  1630. }
  1631. #ifdef CONFIG_COMPAT
  1632. COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
  1633. {
  1634. struct rusage r;
  1635. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
  1636. who != RUSAGE_THREAD)
  1637. return -EINVAL;
  1638. k_getrusage(current, who, &r);
  1639. return put_compat_rusage(&r, ru);
  1640. }
  1641. #endif
  1642. SYSCALL_DEFINE1(umask, int, mask)
  1643. {
  1644. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1645. return mask;
  1646. }
  1647. static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
  1648. {
  1649. struct fd exe;
  1650. struct inode *inode;
  1651. int err;
  1652. exe = fdget(fd);
  1653. if (!exe.file)
  1654. return -EBADF;
  1655. inode = file_inode(exe.file);
  1656. /*
  1657. * Because the original mm->exe_file points to executable file, make
  1658. * sure that this one is executable as well, to avoid breaking an
  1659. * overall picture.
  1660. */
  1661. err = -EACCES;
  1662. if (!S_ISREG(inode->i_mode) ||
  1663. exe.file->f_path.mnt->mnt_flags & MNT_NOEXEC)
  1664. goto exit;
  1665. err = inode_permission(inode, MAY_EXEC);
  1666. if (err)
  1667. goto exit;
  1668. down_write(&mm->mmap_sem);
  1669. /*
  1670. * Forbid mm->exe_file change if old file still mapped.
  1671. */
  1672. err = -EBUSY;
  1673. if (mm->exe_file) {
  1674. struct vm_area_struct *vma;
  1675. for (vma = mm->mmap; vma; vma = vma->vm_next)
  1676. if (vma->vm_file &&
  1677. path_equal(&vma->vm_file->f_path,
  1678. &mm->exe_file->f_path))
  1679. goto exit_unlock;
  1680. }
  1681. /*
  1682. * The symlink can be changed only once, just to disallow arbitrary
  1683. * transitions malicious software might bring in. This means one
  1684. * could make a snapshot over all processes running and monitor
  1685. * /proc/pid/exe changes to notice unusual activity if needed.
  1686. */
  1687. err = -EPERM;
  1688. if (test_and_set_bit(MMF_EXE_FILE_CHANGED, &mm->flags))
  1689. goto exit_unlock;
  1690. err = 0;
  1691. set_mm_exe_file(mm, exe.file); /* this grabs a reference to exe.file */
  1692. exit_unlock:
  1693. up_write(&mm->mmap_sem);
  1694. exit:
  1695. fdput(exe);
  1696. return err;
  1697. }
  1698. static int prctl_set_mm(int opt, unsigned long addr,
  1699. unsigned long arg4, unsigned long arg5)
  1700. {
  1701. unsigned long rlim = rlimit(RLIMIT_DATA);
  1702. struct mm_struct *mm = current->mm;
  1703. struct vm_area_struct *vma;
  1704. int error;
  1705. if (arg5 || (arg4 && opt != PR_SET_MM_AUXV))
  1706. return -EINVAL;
  1707. if (!capable(CAP_SYS_RESOURCE))
  1708. return -EPERM;
  1709. if (opt == PR_SET_MM_EXE_FILE)
  1710. return prctl_set_mm_exe_file(mm, (unsigned int)addr);
  1711. if (addr >= TASK_SIZE || addr < mmap_min_addr)
  1712. return -EINVAL;
  1713. error = -EINVAL;
  1714. down_read(&mm->mmap_sem);
  1715. vma = find_vma(mm, addr);
  1716. switch (opt) {
  1717. case PR_SET_MM_START_CODE:
  1718. mm->start_code = addr;
  1719. break;
  1720. case PR_SET_MM_END_CODE:
  1721. mm->end_code = addr;
  1722. break;
  1723. case PR_SET_MM_START_DATA:
  1724. mm->start_data = addr;
  1725. break;
  1726. case PR_SET_MM_END_DATA:
  1727. mm->end_data = addr;
  1728. break;
  1729. case PR_SET_MM_START_BRK:
  1730. if (addr <= mm->end_data)
  1731. goto out;
  1732. if (rlim < RLIM_INFINITY &&
  1733. (mm->brk - addr) +
  1734. (mm->end_data - mm->start_data) > rlim)
  1735. goto out;
  1736. mm->start_brk = addr;
  1737. break;
  1738. case PR_SET_MM_BRK:
  1739. if (addr <= mm->end_data)
  1740. goto out;
  1741. if (rlim < RLIM_INFINITY &&
  1742. (addr - mm->start_brk) +
  1743. (mm->end_data - mm->start_data) > rlim)
  1744. goto out;
  1745. mm->brk = addr;
  1746. break;
  1747. /*
  1748. * If command line arguments and environment
  1749. * are placed somewhere else on stack, we can
  1750. * set them up here, ARG_START/END to setup
  1751. * command line argumets and ENV_START/END
  1752. * for environment.
  1753. */
  1754. case PR_SET_MM_START_STACK:
  1755. case PR_SET_MM_ARG_START:
  1756. case PR_SET_MM_ARG_END:
  1757. case PR_SET_MM_ENV_START:
  1758. case PR_SET_MM_ENV_END:
  1759. if (!vma) {
  1760. error = -EFAULT;
  1761. goto out;
  1762. }
  1763. if (opt == PR_SET_MM_START_STACK)
  1764. mm->start_stack = addr;
  1765. else if (opt == PR_SET_MM_ARG_START)
  1766. mm->arg_start = addr;
  1767. else if (opt == PR_SET_MM_ARG_END)
  1768. mm->arg_end = addr;
  1769. else if (opt == PR_SET_MM_ENV_START)
  1770. mm->env_start = addr;
  1771. else if (opt == PR_SET_MM_ENV_END)
  1772. mm->env_end = addr;
  1773. break;
  1774. /*
  1775. * This doesn't move auxiliary vector itself
  1776. * since it's pinned to mm_struct, but allow
  1777. * to fill vector with new values. It's up
  1778. * to a caller to provide sane values here
  1779. * otherwise user space tools which use this
  1780. * vector might be unhappy.
  1781. */
  1782. case PR_SET_MM_AUXV: {
  1783. unsigned long user_auxv[AT_VECTOR_SIZE];
  1784. if (arg4 > sizeof(user_auxv))
  1785. goto out;
  1786. up_read(&mm->mmap_sem);
  1787. if (copy_from_user(user_auxv, (const void __user *)addr, arg4))
  1788. return -EFAULT;
  1789. /* Make sure the last entry is always AT_NULL */
  1790. user_auxv[AT_VECTOR_SIZE - 2] = 0;
  1791. user_auxv[AT_VECTOR_SIZE - 1] = 0;
  1792. BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
  1793. task_lock(current);
  1794. memcpy(mm->saved_auxv, user_auxv, arg4);
  1795. task_unlock(current);
  1796. return 0;
  1797. }
  1798. default:
  1799. goto out;
  1800. }
  1801. error = 0;
  1802. out:
  1803. up_read(&mm->mmap_sem);
  1804. return error;
  1805. }
  1806. #ifdef CONFIG_CHECKPOINT_RESTORE
  1807. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1808. {
  1809. return put_user(me->clear_child_tid, tid_addr);
  1810. }
  1811. #else
  1812. static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr)
  1813. {
  1814. return -EINVAL;
  1815. }
  1816. #endif
  1817. SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
  1818. unsigned long, arg4, unsigned long, arg5)
  1819. {
  1820. struct task_struct *me = current;
  1821. unsigned char comm[sizeof(me->comm)];
  1822. long error;
  1823. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1824. if (error != -ENOSYS)
  1825. return error;
  1826. error = 0;
  1827. switch (option) {
  1828. case PR_SET_PDEATHSIG:
  1829. if (!valid_signal(arg2)) {
  1830. error = -EINVAL;
  1831. break;
  1832. }
  1833. me->pdeath_signal = arg2;
  1834. break;
  1835. case PR_GET_PDEATHSIG:
  1836. error = put_user(me->pdeath_signal, (int __user *)arg2);
  1837. break;
  1838. case PR_GET_DUMPABLE:
  1839. error = get_dumpable(me->mm);
  1840. break;
  1841. case PR_SET_DUMPABLE:
  1842. if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
  1843. error = -EINVAL;
  1844. break;
  1845. }
  1846. set_dumpable(me->mm, arg2);
  1847. break;
  1848. case PR_SET_UNALIGN:
  1849. error = SET_UNALIGN_CTL(me, arg2);
  1850. break;
  1851. case PR_GET_UNALIGN:
  1852. error = GET_UNALIGN_CTL(me, arg2);
  1853. break;
  1854. case PR_SET_FPEMU:
  1855. error = SET_FPEMU_CTL(me, arg2);
  1856. break;
  1857. case PR_GET_FPEMU:
  1858. error = GET_FPEMU_CTL(me, arg2);
  1859. break;
  1860. case PR_SET_FPEXC:
  1861. error = SET_FPEXC_CTL(me, arg2);
  1862. break;
  1863. case PR_GET_FPEXC:
  1864. error = GET_FPEXC_CTL(me, arg2);
  1865. break;
  1866. case PR_GET_TIMING:
  1867. error = PR_TIMING_STATISTICAL;
  1868. break;
  1869. case PR_SET_TIMING:
  1870. if (arg2 != PR_TIMING_STATISTICAL)
  1871. error = -EINVAL;
  1872. break;
  1873. case PR_SET_NAME:
  1874. comm[sizeof(me->comm) - 1] = 0;
  1875. if (strncpy_from_user(comm, (char __user *)arg2,
  1876. sizeof(me->comm) - 1) < 0)
  1877. return -EFAULT;
  1878. set_task_comm(me, comm);
  1879. proc_comm_connector(me);
  1880. break;
  1881. case PR_GET_NAME:
  1882. get_task_comm(comm, me);
  1883. if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
  1884. return -EFAULT;
  1885. break;
  1886. case PR_GET_ENDIAN:
  1887. error = GET_ENDIAN(me, arg2);
  1888. break;
  1889. case PR_SET_ENDIAN:
  1890. error = SET_ENDIAN(me, arg2);
  1891. break;
  1892. case PR_GET_SECCOMP:
  1893. error = prctl_get_seccomp();
  1894. break;
  1895. case PR_SET_SECCOMP:
  1896. error = prctl_set_seccomp(arg2, (char __user *)arg3);
  1897. break;
  1898. case PR_GET_TSC:
  1899. error = GET_TSC_CTL(arg2);
  1900. break;
  1901. case PR_SET_TSC:
  1902. error = SET_TSC_CTL(arg2);
  1903. break;
  1904. case PR_TASK_PERF_EVENTS_DISABLE:
  1905. error = perf_event_task_disable();
  1906. break;
  1907. case PR_TASK_PERF_EVENTS_ENABLE:
  1908. error = perf_event_task_enable();
  1909. break;
  1910. case PR_GET_TIMERSLACK:
  1911. error = current->timer_slack_ns;
  1912. break;
  1913. case PR_SET_TIMERSLACK:
  1914. if (arg2 <= 0)
  1915. current->timer_slack_ns =
  1916. current->default_timer_slack_ns;
  1917. else
  1918. current->timer_slack_ns = arg2;
  1919. break;
  1920. case PR_MCE_KILL:
  1921. if (arg4 | arg5)
  1922. return -EINVAL;
  1923. switch (arg2) {
  1924. case PR_MCE_KILL_CLEAR:
  1925. if (arg3 != 0)
  1926. return -EINVAL;
  1927. current->flags &= ~PF_MCE_PROCESS;
  1928. break;
  1929. case PR_MCE_KILL_SET:
  1930. current->flags |= PF_MCE_PROCESS;
  1931. if (arg3 == PR_MCE_KILL_EARLY)
  1932. current->flags |= PF_MCE_EARLY;
  1933. else if (arg3 == PR_MCE_KILL_LATE)
  1934. current->flags &= ~PF_MCE_EARLY;
  1935. else if (arg3 == PR_MCE_KILL_DEFAULT)
  1936. current->flags &=
  1937. ~(PF_MCE_EARLY|PF_MCE_PROCESS);
  1938. else
  1939. return -EINVAL;
  1940. break;
  1941. default:
  1942. return -EINVAL;
  1943. }
  1944. break;
  1945. case PR_MCE_KILL_GET:
  1946. if (arg2 | arg3 | arg4 | arg5)
  1947. return -EINVAL;
  1948. if (current->flags & PF_MCE_PROCESS)
  1949. error = (current->flags & PF_MCE_EARLY) ?
  1950. PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
  1951. else
  1952. error = PR_MCE_KILL_DEFAULT;
  1953. break;
  1954. case PR_SET_MM:
  1955. error = prctl_set_mm(arg2, arg3, arg4, arg5);
  1956. break;
  1957. case PR_GET_TID_ADDRESS:
  1958. error = prctl_get_tid_address(me, (int __user **)arg2);
  1959. break;
  1960. case PR_SET_CHILD_SUBREAPER:
  1961. me->signal->is_child_subreaper = !!arg2;
  1962. break;
  1963. case PR_GET_CHILD_SUBREAPER:
  1964. error = put_user(me->signal->is_child_subreaper,
  1965. (int __user *)arg2);
  1966. break;
  1967. case PR_SET_NO_NEW_PRIVS:
  1968. if (arg2 != 1 || arg3 || arg4 || arg5)
  1969. return -EINVAL;
  1970. current->no_new_privs = 1;
  1971. break;
  1972. case PR_GET_NO_NEW_PRIVS:
  1973. if (arg2 || arg3 || arg4 || arg5)
  1974. return -EINVAL;
  1975. return current->no_new_privs ? 1 : 0;
  1976. default:
  1977. error = -EINVAL;
  1978. break;
  1979. }
  1980. return error;
  1981. }
  1982. SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
  1983. struct getcpu_cache __user *, unused)
  1984. {
  1985. int err = 0;
  1986. int cpu = raw_smp_processor_id();
  1987. if (cpup)
  1988. err |= put_user(cpu, cpup);
  1989. if (nodep)
  1990. err |= put_user(cpu_to_node(cpu), nodep);
  1991. return err ? -EFAULT : 0;
  1992. }
  1993. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1994. static int __orderly_poweroff(bool force)
  1995. {
  1996. char **argv;
  1997. static char *envp[] = {
  1998. "HOME=/",
  1999. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  2000. NULL
  2001. };
  2002. int ret;
  2003. argv = argv_split(GFP_KERNEL, poweroff_cmd, NULL);
  2004. if (argv) {
  2005. ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_EXEC);
  2006. argv_free(argv);
  2007. } else {
  2008. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  2009. __func__, poweroff_cmd);
  2010. ret = -ENOMEM;
  2011. }
  2012. if (ret && force) {
  2013. printk(KERN_WARNING "Failed to start orderly shutdown: "
  2014. "forcing the issue\n");
  2015. /*
  2016. * I guess this should try to kick off some daemon to sync and
  2017. * poweroff asap. Or not even bother syncing if we're doing an
  2018. * emergency shutdown?
  2019. */
  2020. emergency_sync();
  2021. kernel_power_off();
  2022. }
  2023. return ret;
  2024. }
  2025. static bool poweroff_force;
  2026. static void poweroff_work_func(struct work_struct *work)
  2027. {
  2028. __orderly_poweroff(poweroff_force);
  2029. }
  2030. static DECLARE_WORK(poweroff_work, poweroff_work_func);
  2031. /**
  2032. * orderly_poweroff - Trigger an orderly system poweroff
  2033. * @force: force poweroff if command execution fails
  2034. *
  2035. * This may be called from any context to trigger a system shutdown.
  2036. * If the orderly shutdown fails, it will force an immediate shutdown.
  2037. */
  2038. int orderly_poweroff(bool force)
  2039. {
  2040. if (force) /* do not override the pending "true" */
  2041. poweroff_force = true;
  2042. schedule_work(&poweroff_work);
  2043. return 0;
  2044. }
  2045. EXPORT_SYMBOL_GPL(orderly_poweroff);
  2046. /**
  2047. * do_sysinfo - fill in sysinfo struct
  2048. * @info: pointer to buffer to fill
  2049. */
  2050. static int do_sysinfo(struct sysinfo *info)
  2051. {
  2052. unsigned long mem_total, sav_total;
  2053. unsigned int mem_unit, bitcount;
  2054. struct timespec tp;
  2055. memset(info, 0, sizeof(struct sysinfo));
  2056. get_monotonic_boottime(&tp);
  2057. info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  2058. get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
  2059. info->procs = nr_threads;
  2060. si_meminfo(info);
  2061. si_swapinfo(info);
  2062. /*
  2063. * If the sum of all the available memory (i.e. ram + swap)
  2064. * is less than can be stored in a 32 bit unsigned long then
  2065. * we can be binary compatible with 2.2.x kernels. If not,
  2066. * well, in that case 2.2.x was broken anyways...
  2067. *
  2068. * -Erik Andersen <andersee@debian.org>
  2069. */
  2070. mem_total = info->totalram + info->totalswap;
  2071. if (mem_total < info->totalram || mem_total < info->totalswap)
  2072. goto out;
  2073. bitcount = 0;
  2074. mem_unit = info->mem_unit;
  2075. while (mem_unit > 1) {
  2076. bitcount++;
  2077. mem_unit >>= 1;
  2078. sav_total = mem_total;
  2079. mem_total <<= 1;
  2080. if (mem_total < sav_total)
  2081. goto out;
  2082. }
  2083. /*
  2084. * If mem_total did not overflow, multiply all memory values by
  2085. * info->mem_unit and set it to 1. This leaves things compatible
  2086. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  2087. * kernels...
  2088. */
  2089. info->mem_unit = 1;
  2090. info->totalram <<= bitcount;
  2091. info->freeram <<= bitcount;
  2092. info->sharedram <<= bitcount;
  2093. info->bufferram <<= bitcount;
  2094. info->totalswap <<= bitcount;
  2095. info->freeswap <<= bitcount;
  2096. info->totalhigh <<= bitcount;
  2097. info->freehigh <<= bitcount;
  2098. out:
  2099. return 0;
  2100. }
  2101. SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
  2102. {
  2103. struct sysinfo val;
  2104. do_sysinfo(&val);
  2105. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  2106. return -EFAULT;
  2107. return 0;
  2108. }
  2109. #ifdef CONFIG_COMPAT
  2110. struct compat_sysinfo {
  2111. s32 uptime;
  2112. u32 loads[3];
  2113. u32 totalram;
  2114. u32 freeram;
  2115. u32 sharedram;
  2116. u32 bufferram;
  2117. u32 totalswap;
  2118. u32 freeswap;
  2119. u16 procs;
  2120. u16 pad;
  2121. u32 totalhigh;
  2122. u32 freehigh;
  2123. u32 mem_unit;
  2124. char _f[20-2*sizeof(u32)-sizeof(int)];
  2125. };
  2126. COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
  2127. {
  2128. struct sysinfo s;
  2129. do_sysinfo(&s);
  2130. /* Check to see if any memory value is too large for 32-bit and scale
  2131. * down if needed
  2132. */
  2133. if ((s.totalram >> 32) || (s.totalswap >> 32)) {
  2134. int bitcount = 0;
  2135. while (s.mem_unit < PAGE_SIZE) {
  2136. s.mem_unit <<= 1;
  2137. bitcount++;
  2138. }
  2139. s.totalram >>= bitcount;
  2140. s.freeram >>= bitcount;
  2141. s.sharedram >>= bitcount;
  2142. s.bufferram >>= bitcount;
  2143. s.totalswap >>= bitcount;
  2144. s.freeswap >>= bitcount;
  2145. s.totalhigh >>= bitcount;
  2146. s.freehigh >>= bitcount;
  2147. }
  2148. if (!access_ok(VERIFY_WRITE, info, sizeof(struct compat_sysinfo)) ||
  2149. __put_user(s.uptime, &info->uptime) ||
  2150. __put_user(s.loads[0], &info->loads[0]) ||
  2151. __put_user(s.loads[1], &info->loads[1]) ||
  2152. __put_user(s.loads[2], &info->loads[2]) ||
  2153. __put_user(s.totalram, &info->totalram) ||
  2154. __put_user(s.freeram, &info->freeram) ||
  2155. __put_user(s.sharedram, &info->sharedram) ||
  2156. __put_user(s.bufferram, &info->bufferram) ||
  2157. __put_user(s.totalswap, &info->totalswap) ||
  2158. __put_user(s.freeswap, &info->freeswap) ||
  2159. __put_user(s.procs, &info->procs) ||
  2160. __put_user(s.totalhigh, &info->totalhigh) ||
  2161. __put_user(s.freehigh, &info->freehigh) ||
  2162. __put_user(s.mem_unit, &info->mem_unit))
  2163. return -EFAULT;
  2164. return 0;
  2165. }
  2166. #endif /* CONFIG_COMPAT */