sys.c 64 KB

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