sys.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /*
  2. * linux/kernel/sys.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mm.h>
  8. #include <linux/utsname.h>
  9. #include <linux/mman.h>
  10. #include <linux/smp_lock.h>
  11. #include <linux/notifier.h>
  12. #include <linux/reboot.h>
  13. #include <linux/prctl.h>
  14. #include <linux/highuid.h>
  15. #include <linux/fs.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/compat.h>
  36. #include <linux/syscalls.h>
  37. #include <linux/kprobes.h>
  38. #include <linux/user_namespace.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/io.h>
  41. #include <asm/unistd.h>
  42. #ifndef SET_UNALIGN_CTL
  43. # define SET_UNALIGN_CTL(a,b) (-EINVAL)
  44. #endif
  45. #ifndef GET_UNALIGN_CTL
  46. # define GET_UNALIGN_CTL(a,b) (-EINVAL)
  47. #endif
  48. #ifndef SET_FPEMU_CTL
  49. # define SET_FPEMU_CTL(a,b) (-EINVAL)
  50. #endif
  51. #ifndef GET_FPEMU_CTL
  52. # define GET_FPEMU_CTL(a,b) (-EINVAL)
  53. #endif
  54. #ifndef SET_FPEXC_CTL
  55. # define SET_FPEXC_CTL(a,b) (-EINVAL)
  56. #endif
  57. #ifndef GET_FPEXC_CTL
  58. # define GET_FPEXC_CTL(a,b) (-EINVAL)
  59. #endif
  60. #ifndef GET_ENDIAN
  61. # define GET_ENDIAN(a,b) (-EINVAL)
  62. #endif
  63. #ifndef SET_ENDIAN
  64. # define SET_ENDIAN(a,b) (-EINVAL)
  65. #endif
  66. /*
  67. * this is where the system-wide overflow UID and GID are defined, for
  68. * architectures that now have 32-bit UID/GID but didn't in the past
  69. */
  70. int overflowuid = DEFAULT_OVERFLOWUID;
  71. int overflowgid = DEFAULT_OVERFLOWGID;
  72. #ifdef CONFIG_UID16
  73. EXPORT_SYMBOL(overflowuid);
  74. EXPORT_SYMBOL(overflowgid);
  75. #endif
  76. /*
  77. * the same as above, but for filesystems which can only store a 16-bit
  78. * UID and GID. as such, this is needed on all architectures
  79. */
  80. int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
  81. int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
  82. EXPORT_SYMBOL(fs_overflowuid);
  83. EXPORT_SYMBOL(fs_overflowgid);
  84. /*
  85. * this indicates whether you can reboot with ctrl-alt-del: the default is yes
  86. */
  87. int C_A_D = 1;
  88. struct pid *cad_pid;
  89. EXPORT_SYMBOL(cad_pid);
  90. /*
  91. * If set, this is used for preparing the system to power off.
  92. */
  93. void (*pm_power_off_prepare)(void);
  94. EXPORT_SYMBOL(pm_power_off_prepare);
  95. /*
  96. * Notifier list for kernel code which wants to be called
  97. * at shutdown. This is used to stop any idling DMA operations
  98. * and the like.
  99. */
  100. static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
  101. /*
  102. * Notifier chain core routines. The exported routines below
  103. * are layered on top of these, with appropriate locking added.
  104. */
  105. static int notifier_chain_register(struct notifier_block **nl,
  106. struct notifier_block *n)
  107. {
  108. while ((*nl) != NULL) {
  109. if (n->priority > (*nl)->priority)
  110. break;
  111. nl = &((*nl)->next);
  112. }
  113. n->next = *nl;
  114. rcu_assign_pointer(*nl, n);
  115. return 0;
  116. }
  117. static int notifier_chain_unregister(struct notifier_block **nl,
  118. struct notifier_block *n)
  119. {
  120. while ((*nl) != NULL) {
  121. if ((*nl) == n) {
  122. rcu_assign_pointer(*nl, n->next);
  123. return 0;
  124. }
  125. nl = &((*nl)->next);
  126. }
  127. return -ENOENT;
  128. }
  129. /**
  130. * notifier_call_chain - Informs the registered notifiers about an event.
  131. * @nl: Pointer to head of the blocking notifier chain
  132. * @val: Value passed unmodified to notifier function
  133. * @v: Pointer passed unmodified to notifier function
  134. * @nr_to_call: Number of notifier functions to be called. Don't care
  135. * value of this parameter is -1.
  136. * @nr_calls: Records the number of notifications sent. Don't care
  137. * value of this field is NULL.
  138. * @returns: notifier_call_chain returns the value returned by the
  139. * last notifier function called.
  140. */
  141. static int __kprobes notifier_call_chain(struct notifier_block **nl,
  142. unsigned long val, void *v,
  143. int nr_to_call, int *nr_calls)
  144. {
  145. int ret = NOTIFY_DONE;
  146. struct notifier_block *nb, *next_nb;
  147. nb = rcu_dereference(*nl);
  148. while (nb && nr_to_call) {
  149. next_nb = rcu_dereference(nb->next);
  150. ret = nb->notifier_call(nb, val, v);
  151. if (nr_calls)
  152. (*nr_calls)++;
  153. if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
  154. break;
  155. nb = next_nb;
  156. nr_to_call--;
  157. }
  158. return ret;
  159. }
  160. /*
  161. * Atomic notifier chain routines. Registration and unregistration
  162. * use a spinlock, and call_chain is synchronized by RCU (no locks).
  163. */
  164. /**
  165. * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
  166. * @nh: Pointer to head of the atomic notifier chain
  167. * @n: New entry in notifier chain
  168. *
  169. * Adds a notifier to an atomic notifier chain.
  170. *
  171. * Currently always returns zero.
  172. */
  173. int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
  174. struct notifier_block *n)
  175. {
  176. unsigned long flags;
  177. int ret;
  178. spin_lock_irqsave(&nh->lock, flags);
  179. ret = notifier_chain_register(&nh->head, n);
  180. spin_unlock_irqrestore(&nh->lock, flags);
  181. return ret;
  182. }
  183. EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
  184. /**
  185. * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
  186. * @nh: Pointer to head of the atomic notifier chain
  187. * @n: Entry to remove from notifier chain
  188. *
  189. * Removes a notifier from an atomic notifier chain.
  190. *
  191. * Returns zero on success or %-ENOENT on failure.
  192. */
  193. int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
  194. struct notifier_block *n)
  195. {
  196. unsigned long flags;
  197. int ret;
  198. spin_lock_irqsave(&nh->lock, flags);
  199. ret = notifier_chain_unregister(&nh->head, n);
  200. spin_unlock_irqrestore(&nh->lock, flags);
  201. synchronize_rcu();
  202. return ret;
  203. }
  204. EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
  205. /**
  206. * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
  207. * @nh: Pointer to head of the atomic notifier chain
  208. * @val: Value passed unmodified to notifier function
  209. * @v: Pointer passed unmodified to notifier function
  210. * @nr_to_call: See the comment for notifier_call_chain.
  211. * @nr_calls: See the comment for notifier_call_chain.
  212. *
  213. * Calls each function in a notifier chain in turn. The functions
  214. * run in an atomic context, so they must not block.
  215. * This routine uses RCU to synchronize with changes to the chain.
  216. *
  217. * If the return value of the notifier can be and'ed
  218. * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
  219. * will return immediately, with the return value of
  220. * the notifier function which halted execution.
  221. * Otherwise the return value is the return value
  222. * of the last notifier function called.
  223. */
  224. int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  225. unsigned long val, void *v,
  226. int nr_to_call, int *nr_calls)
  227. {
  228. int ret;
  229. rcu_read_lock();
  230. ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  231. rcu_read_unlock();
  232. return ret;
  233. }
  234. EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
  235. int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
  236. unsigned long val, void *v)
  237. {
  238. return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
  239. }
  240. EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
  241. /*
  242. * Blocking notifier chain routines. All access to the chain is
  243. * synchronized by an rwsem.
  244. */
  245. /**
  246. * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
  247. * @nh: Pointer to head of the blocking notifier chain
  248. * @n: New entry in notifier chain
  249. *
  250. * Adds a notifier to a blocking notifier chain.
  251. * Must be called in process context.
  252. *
  253. * Currently always returns zero.
  254. */
  255. int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
  256. struct notifier_block *n)
  257. {
  258. int ret;
  259. /*
  260. * This code gets used during boot-up, when task switching is
  261. * not yet working and interrupts must remain disabled. At
  262. * such times we must not call down_write().
  263. */
  264. if (unlikely(system_state == SYSTEM_BOOTING))
  265. return notifier_chain_register(&nh->head, n);
  266. down_write(&nh->rwsem);
  267. ret = notifier_chain_register(&nh->head, n);
  268. up_write(&nh->rwsem);
  269. return ret;
  270. }
  271. EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
  272. /**
  273. * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
  274. * @nh: Pointer to head of the blocking notifier chain
  275. * @n: Entry to remove from notifier chain
  276. *
  277. * Removes a notifier from a blocking notifier chain.
  278. * Must be called from process context.
  279. *
  280. * Returns zero on success or %-ENOENT on failure.
  281. */
  282. int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
  283. struct notifier_block *n)
  284. {
  285. int ret;
  286. /*
  287. * This code gets used during boot-up, when task switching is
  288. * not yet working and interrupts must remain disabled. At
  289. * such times we must not call down_write().
  290. */
  291. if (unlikely(system_state == SYSTEM_BOOTING))
  292. return notifier_chain_unregister(&nh->head, n);
  293. down_write(&nh->rwsem);
  294. ret = notifier_chain_unregister(&nh->head, n);
  295. up_write(&nh->rwsem);
  296. return ret;
  297. }
  298. EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
  299. /**
  300. * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
  301. * @nh: Pointer to head of the blocking notifier chain
  302. * @val: Value passed unmodified to notifier function
  303. * @v: Pointer passed unmodified to notifier function
  304. * @nr_to_call: See comment for notifier_call_chain.
  305. * @nr_calls: See comment for notifier_call_chain.
  306. *
  307. * Calls each function in a notifier chain in turn. The functions
  308. * run in a process context, so they are allowed to block.
  309. *
  310. * If the return value of the notifier can be and'ed
  311. * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
  312. * will return immediately, with the return value of
  313. * the notifier function which halted execution.
  314. * Otherwise the return value is the return value
  315. * of the last notifier function called.
  316. */
  317. int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  318. unsigned long val, void *v,
  319. int nr_to_call, int *nr_calls)
  320. {
  321. int ret = NOTIFY_DONE;
  322. /*
  323. * We check the head outside the lock, but if this access is
  324. * racy then it does not matter what the result of the test
  325. * is, we re-check the list after having taken the lock anyway:
  326. */
  327. if (rcu_dereference(nh->head)) {
  328. down_read(&nh->rwsem);
  329. ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
  330. nr_calls);
  331. up_read(&nh->rwsem);
  332. }
  333. return ret;
  334. }
  335. EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
  336. int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
  337. unsigned long val, void *v)
  338. {
  339. return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
  340. }
  341. EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
  342. /*
  343. * Raw notifier chain routines. There is no protection;
  344. * the caller must provide it. Use at your own risk!
  345. */
  346. /**
  347. * raw_notifier_chain_register - Add notifier to a raw notifier chain
  348. * @nh: Pointer to head of the raw notifier chain
  349. * @n: New entry in notifier chain
  350. *
  351. * Adds a notifier to a raw notifier chain.
  352. * All locking must be provided by the caller.
  353. *
  354. * Currently always returns zero.
  355. */
  356. int raw_notifier_chain_register(struct raw_notifier_head *nh,
  357. struct notifier_block *n)
  358. {
  359. return notifier_chain_register(&nh->head, n);
  360. }
  361. EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
  362. /**
  363. * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
  364. * @nh: Pointer to head of the raw notifier chain
  365. * @n: Entry to remove from notifier chain
  366. *
  367. * Removes a notifier from a raw notifier chain.
  368. * All locking must be provided by the caller.
  369. *
  370. * Returns zero on success or %-ENOENT on failure.
  371. */
  372. int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
  373. struct notifier_block *n)
  374. {
  375. return notifier_chain_unregister(&nh->head, n);
  376. }
  377. EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
  378. /**
  379. * __raw_notifier_call_chain - Call functions in a raw notifier chain
  380. * @nh: Pointer to head of the raw notifier chain
  381. * @val: Value passed unmodified to notifier function
  382. * @v: Pointer passed unmodified to notifier function
  383. * @nr_to_call: See comment for notifier_call_chain.
  384. * @nr_calls: See comment for notifier_call_chain
  385. *
  386. * Calls each function in a notifier chain in turn. The functions
  387. * run in an undefined context.
  388. * All locking must be provided by the caller.
  389. *
  390. * If the return value of the notifier can be and'ed
  391. * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
  392. * will return immediately, with the return value of
  393. * the notifier function which halted execution.
  394. * Otherwise the return value is the return value
  395. * of the last notifier function called.
  396. */
  397. int __raw_notifier_call_chain(struct raw_notifier_head *nh,
  398. unsigned long val, void *v,
  399. int nr_to_call, int *nr_calls)
  400. {
  401. return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  402. }
  403. EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
  404. int raw_notifier_call_chain(struct raw_notifier_head *nh,
  405. unsigned long val, void *v)
  406. {
  407. return __raw_notifier_call_chain(nh, val, v, -1, NULL);
  408. }
  409. EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
  410. /*
  411. * SRCU notifier chain routines. Registration and unregistration
  412. * use a mutex, and call_chain is synchronized by SRCU (no locks).
  413. */
  414. /**
  415. * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
  416. * @nh: Pointer to head of the SRCU notifier chain
  417. * @n: New entry in notifier chain
  418. *
  419. * Adds a notifier to an SRCU notifier chain.
  420. * Must be called in process context.
  421. *
  422. * Currently always returns zero.
  423. */
  424. int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
  425. struct notifier_block *n)
  426. {
  427. int ret;
  428. /*
  429. * This code gets used during boot-up, when task switching is
  430. * not yet working and interrupts must remain disabled. At
  431. * such times we must not call mutex_lock().
  432. */
  433. if (unlikely(system_state == SYSTEM_BOOTING))
  434. return notifier_chain_register(&nh->head, n);
  435. mutex_lock(&nh->mutex);
  436. ret = notifier_chain_register(&nh->head, n);
  437. mutex_unlock(&nh->mutex);
  438. return ret;
  439. }
  440. EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
  441. /**
  442. * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
  443. * @nh: Pointer to head of the SRCU notifier chain
  444. * @n: Entry to remove from notifier chain
  445. *
  446. * Removes a notifier from an SRCU notifier chain.
  447. * Must be called from process context.
  448. *
  449. * Returns zero on success or %-ENOENT on failure.
  450. */
  451. int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
  452. struct notifier_block *n)
  453. {
  454. int ret;
  455. /*
  456. * This code gets used during boot-up, when task switching is
  457. * not yet working and interrupts must remain disabled. At
  458. * such times we must not call mutex_lock().
  459. */
  460. if (unlikely(system_state == SYSTEM_BOOTING))
  461. return notifier_chain_unregister(&nh->head, n);
  462. mutex_lock(&nh->mutex);
  463. ret = notifier_chain_unregister(&nh->head, n);
  464. mutex_unlock(&nh->mutex);
  465. synchronize_srcu(&nh->srcu);
  466. return ret;
  467. }
  468. EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
  469. /**
  470. * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
  471. * @nh: Pointer to head of the SRCU notifier chain
  472. * @val: Value passed unmodified to notifier function
  473. * @v: Pointer passed unmodified to notifier function
  474. * @nr_to_call: See comment for notifier_call_chain.
  475. * @nr_calls: See comment for notifier_call_chain
  476. *
  477. * Calls each function in a notifier chain in turn. The functions
  478. * run in a process context, so they are allowed to block.
  479. *
  480. * If the return value of the notifier can be and'ed
  481. * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
  482. * will return immediately, with the return value of
  483. * the notifier function which halted execution.
  484. * Otherwise the return value is the return value
  485. * of the last notifier function called.
  486. */
  487. int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
  488. unsigned long val, void *v,
  489. int nr_to_call, int *nr_calls)
  490. {
  491. int ret;
  492. int idx;
  493. idx = srcu_read_lock(&nh->srcu);
  494. ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
  495. srcu_read_unlock(&nh->srcu, idx);
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
  499. int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
  500. unsigned long val, void *v)
  501. {
  502. return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
  503. }
  504. EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
  505. /**
  506. * srcu_init_notifier_head - Initialize an SRCU notifier head
  507. * @nh: Pointer to head of the srcu notifier chain
  508. *
  509. * Unlike other sorts of notifier heads, SRCU notifier heads require
  510. * dynamic initialization. Be sure to call this routine before
  511. * calling any of the other SRCU notifier routines for this head.
  512. *
  513. * If an SRCU notifier head is deallocated, it must first be cleaned
  514. * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
  515. * per-cpu data (used by the SRCU mechanism) will leak.
  516. */
  517. void srcu_init_notifier_head(struct srcu_notifier_head *nh)
  518. {
  519. mutex_init(&nh->mutex);
  520. if (init_srcu_struct(&nh->srcu) < 0)
  521. BUG();
  522. nh->head = NULL;
  523. }
  524. EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
  525. /**
  526. * register_reboot_notifier - Register function to be called at reboot time
  527. * @nb: Info about notifier function to be called
  528. *
  529. * Registers a function with the list of functions
  530. * to be called at reboot time.
  531. *
  532. * Currently always returns zero, as blocking_notifier_chain_register()
  533. * always returns zero.
  534. */
  535. int register_reboot_notifier(struct notifier_block * nb)
  536. {
  537. return blocking_notifier_chain_register(&reboot_notifier_list, nb);
  538. }
  539. EXPORT_SYMBOL(register_reboot_notifier);
  540. /**
  541. * unregister_reboot_notifier - Unregister previously registered reboot notifier
  542. * @nb: Hook to be unregistered
  543. *
  544. * Unregisters a previously registered reboot
  545. * notifier function.
  546. *
  547. * Returns zero on success, or %-ENOENT on failure.
  548. */
  549. int unregister_reboot_notifier(struct notifier_block * nb)
  550. {
  551. return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
  552. }
  553. EXPORT_SYMBOL(unregister_reboot_notifier);
  554. static int set_one_prio(struct task_struct *p, int niceval, int error)
  555. {
  556. int no_nice;
  557. if (p->uid != current->euid &&
  558. p->euid != current->euid && !capable(CAP_SYS_NICE)) {
  559. error = -EPERM;
  560. goto out;
  561. }
  562. if (niceval < task_nice(p) && !can_nice(p, niceval)) {
  563. error = -EACCES;
  564. goto out;
  565. }
  566. no_nice = security_task_setnice(p, niceval);
  567. if (no_nice) {
  568. error = no_nice;
  569. goto out;
  570. }
  571. if (error == -ESRCH)
  572. error = 0;
  573. set_user_nice(p, niceval);
  574. out:
  575. return error;
  576. }
  577. asmlinkage long sys_setpriority(int which, int who, int niceval)
  578. {
  579. struct task_struct *g, *p;
  580. struct user_struct *user;
  581. int error = -EINVAL;
  582. struct pid *pgrp;
  583. if (which > PRIO_USER || which < PRIO_PROCESS)
  584. goto out;
  585. /* normalize: avoid signed division (rounding problems) */
  586. error = -ESRCH;
  587. if (niceval < -20)
  588. niceval = -20;
  589. if (niceval > 19)
  590. niceval = 19;
  591. read_lock(&tasklist_lock);
  592. switch (which) {
  593. case PRIO_PROCESS:
  594. if (who)
  595. p = find_task_by_pid(who);
  596. else
  597. p = current;
  598. if (p)
  599. error = set_one_prio(p, niceval, error);
  600. break;
  601. case PRIO_PGRP:
  602. if (who)
  603. pgrp = find_pid(who);
  604. else
  605. pgrp = task_pgrp(current);
  606. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  607. error = set_one_prio(p, niceval, error);
  608. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  609. break;
  610. case PRIO_USER:
  611. user = current->user;
  612. if (!who)
  613. who = current->uid;
  614. else
  615. if ((who != current->uid) && !(user = find_user(who)))
  616. goto out_unlock; /* No processes for this user */
  617. do_each_thread(g, p)
  618. if (p->uid == who)
  619. error = set_one_prio(p, niceval, error);
  620. while_each_thread(g, p);
  621. if (who != current->uid)
  622. free_uid(user); /* For find_user() */
  623. break;
  624. }
  625. out_unlock:
  626. read_unlock(&tasklist_lock);
  627. out:
  628. return error;
  629. }
  630. /*
  631. * Ugh. To avoid negative return values, "getpriority()" will
  632. * not return the normal nice-value, but a negated value that
  633. * has been offset by 20 (ie it returns 40..1 instead of -20..19)
  634. * to stay compatible.
  635. */
  636. asmlinkage long sys_getpriority(int which, int who)
  637. {
  638. struct task_struct *g, *p;
  639. struct user_struct *user;
  640. long niceval, retval = -ESRCH;
  641. struct pid *pgrp;
  642. if (which > PRIO_USER || which < PRIO_PROCESS)
  643. return -EINVAL;
  644. read_lock(&tasklist_lock);
  645. switch (which) {
  646. case PRIO_PROCESS:
  647. if (who)
  648. p = find_task_by_pid(who);
  649. else
  650. p = current;
  651. if (p) {
  652. niceval = 20 - task_nice(p);
  653. if (niceval > retval)
  654. retval = niceval;
  655. }
  656. break;
  657. case PRIO_PGRP:
  658. if (who)
  659. pgrp = find_pid(who);
  660. else
  661. pgrp = task_pgrp(current);
  662. do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
  663. niceval = 20 - task_nice(p);
  664. if (niceval > retval)
  665. retval = niceval;
  666. } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
  667. break;
  668. case PRIO_USER:
  669. user = current->user;
  670. if (!who)
  671. who = current->uid;
  672. else
  673. if ((who != current->uid) && !(user = find_user(who)))
  674. goto out_unlock; /* No processes for this user */
  675. do_each_thread(g, p)
  676. if (p->uid == who) {
  677. niceval = 20 - task_nice(p);
  678. if (niceval > retval)
  679. retval = niceval;
  680. }
  681. while_each_thread(g, p);
  682. if (who != current->uid)
  683. free_uid(user); /* for find_user() */
  684. break;
  685. }
  686. out_unlock:
  687. read_unlock(&tasklist_lock);
  688. return retval;
  689. }
  690. /**
  691. * emergency_restart - reboot the system
  692. *
  693. * Without shutting down any hardware or taking any locks
  694. * reboot the system. This is called when we know we are in
  695. * trouble so this is our best effort to reboot. This is
  696. * safe to call in interrupt context.
  697. */
  698. void emergency_restart(void)
  699. {
  700. machine_emergency_restart();
  701. }
  702. EXPORT_SYMBOL_GPL(emergency_restart);
  703. static void kernel_restart_prepare(char *cmd)
  704. {
  705. blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
  706. system_state = SYSTEM_RESTART;
  707. device_shutdown();
  708. sysdev_shutdown();
  709. }
  710. /**
  711. * kernel_restart - reboot the system
  712. * @cmd: pointer to buffer containing command to execute for restart
  713. * or %NULL
  714. *
  715. * Shutdown everything and perform a clean reboot.
  716. * This is not safe to call in interrupt context.
  717. */
  718. void kernel_restart(char *cmd)
  719. {
  720. kernel_restart_prepare(cmd);
  721. if (!cmd)
  722. printk(KERN_EMERG "Restarting system.\n");
  723. else
  724. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  725. machine_restart(cmd);
  726. }
  727. EXPORT_SYMBOL_GPL(kernel_restart);
  728. /**
  729. * kernel_kexec - reboot the system
  730. *
  731. * Move into place and start executing a preloaded standalone
  732. * executable. If nothing was preloaded return an error.
  733. */
  734. static void kernel_kexec(void)
  735. {
  736. #ifdef CONFIG_KEXEC
  737. struct kimage *image;
  738. image = xchg(&kexec_image, NULL);
  739. if (!image)
  740. return;
  741. kernel_restart_prepare(NULL);
  742. printk(KERN_EMERG "Starting new kernel\n");
  743. machine_shutdown();
  744. machine_kexec(image);
  745. #endif
  746. }
  747. void kernel_shutdown_prepare(enum system_states state)
  748. {
  749. blocking_notifier_call_chain(&reboot_notifier_list,
  750. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  751. system_state = state;
  752. device_shutdown();
  753. }
  754. /**
  755. * kernel_halt - halt the system
  756. *
  757. * Shutdown everything and perform a clean system halt.
  758. */
  759. void kernel_halt(void)
  760. {
  761. kernel_shutdown_prepare(SYSTEM_HALT);
  762. sysdev_shutdown();
  763. printk(KERN_EMERG "System halted.\n");
  764. machine_halt();
  765. }
  766. EXPORT_SYMBOL_GPL(kernel_halt);
  767. /**
  768. * kernel_power_off - power_off the system
  769. *
  770. * Shutdown everything and perform a clean system power_off.
  771. */
  772. void kernel_power_off(void)
  773. {
  774. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  775. if (pm_power_off_prepare)
  776. pm_power_off_prepare();
  777. disable_nonboot_cpus();
  778. sysdev_shutdown();
  779. printk(KERN_EMERG "Power down.\n");
  780. machine_power_off();
  781. }
  782. EXPORT_SYMBOL_GPL(kernel_power_off);
  783. /*
  784. * Reboot system call: for obvious reasons only root may call it,
  785. * and even root needs to set up some magic numbers in the registers
  786. * so that some mistake won't make this reboot the whole machine.
  787. * You can also set the meaning of the ctrl-alt-del-key here.
  788. *
  789. * reboot doesn't sync: do that yourself before calling this.
  790. */
  791. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  792. {
  793. char buffer[256];
  794. /* We only trust the superuser with rebooting the system. */
  795. if (!capable(CAP_SYS_BOOT))
  796. return -EPERM;
  797. /* For safety, we require "magic" arguments. */
  798. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  799. (magic2 != LINUX_REBOOT_MAGIC2 &&
  800. magic2 != LINUX_REBOOT_MAGIC2A &&
  801. magic2 != LINUX_REBOOT_MAGIC2B &&
  802. magic2 != LINUX_REBOOT_MAGIC2C))
  803. return -EINVAL;
  804. /* Instead of trying to make the power_off code look like
  805. * halt when pm_power_off is not set do it the easy way.
  806. */
  807. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  808. cmd = LINUX_REBOOT_CMD_HALT;
  809. lock_kernel();
  810. switch (cmd) {
  811. case LINUX_REBOOT_CMD_RESTART:
  812. kernel_restart(NULL);
  813. break;
  814. case LINUX_REBOOT_CMD_CAD_ON:
  815. C_A_D = 1;
  816. break;
  817. case LINUX_REBOOT_CMD_CAD_OFF:
  818. C_A_D = 0;
  819. break;
  820. case LINUX_REBOOT_CMD_HALT:
  821. kernel_halt();
  822. unlock_kernel();
  823. do_exit(0);
  824. break;
  825. case LINUX_REBOOT_CMD_POWER_OFF:
  826. kernel_power_off();
  827. unlock_kernel();
  828. do_exit(0);
  829. break;
  830. case LINUX_REBOOT_CMD_RESTART2:
  831. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  832. unlock_kernel();
  833. return -EFAULT;
  834. }
  835. buffer[sizeof(buffer) - 1] = '\0';
  836. kernel_restart(buffer);
  837. break;
  838. case LINUX_REBOOT_CMD_KEXEC:
  839. kernel_kexec();
  840. unlock_kernel();
  841. return -EINVAL;
  842. #ifdef CONFIG_HIBERNATION
  843. case LINUX_REBOOT_CMD_SW_SUSPEND:
  844. {
  845. int ret = hibernate();
  846. unlock_kernel();
  847. return ret;
  848. }
  849. #endif
  850. default:
  851. unlock_kernel();
  852. return -EINVAL;
  853. }
  854. unlock_kernel();
  855. return 0;
  856. }
  857. static void deferred_cad(struct work_struct *dummy)
  858. {
  859. kernel_restart(NULL);
  860. }
  861. /*
  862. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  863. * As it's called within an interrupt, it may NOT sync: the only choice
  864. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  865. */
  866. void ctrl_alt_del(void)
  867. {
  868. static DECLARE_WORK(cad_work, deferred_cad);
  869. if (C_A_D)
  870. schedule_work(&cad_work);
  871. else
  872. kill_cad_pid(SIGINT, 1);
  873. }
  874. /*
  875. * Unprivileged users may change the real gid to the effective gid
  876. * or vice versa. (BSD-style)
  877. *
  878. * If you set the real gid at all, or set the effective gid to a value not
  879. * equal to the real gid, then the saved gid is set to the new effective gid.
  880. *
  881. * This makes it possible for a setgid program to completely drop its
  882. * privileges, which is often a useful assertion to make when you are doing
  883. * a security audit over a program.
  884. *
  885. * The general idea is that a program which uses just setregid() will be
  886. * 100% compatible with BSD. A program which uses just setgid() will be
  887. * 100% compatible with POSIX with saved IDs.
  888. *
  889. * SMP: There are not races, the GIDs are checked only by filesystem
  890. * operations (as far as semantic preservation is concerned).
  891. */
  892. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  893. {
  894. int old_rgid = current->gid;
  895. int old_egid = current->egid;
  896. int new_rgid = old_rgid;
  897. int new_egid = old_egid;
  898. int retval;
  899. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  900. if (retval)
  901. return retval;
  902. if (rgid != (gid_t) -1) {
  903. if ((old_rgid == rgid) ||
  904. (current->egid==rgid) ||
  905. capable(CAP_SETGID))
  906. new_rgid = rgid;
  907. else
  908. return -EPERM;
  909. }
  910. if (egid != (gid_t) -1) {
  911. if ((old_rgid == egid) ||
  912. (current->egid == egid) ||
  913. (current->sgid == egid) ||
  914. capable(CAP_SETGID))
  915. new_egid = egid;
  916. else
  917. return -EPERM;
  918. }
  919. if (new_egid != old_egid) {
  920. set_dumpable(current->mm, suid_dumpable);
  921. smp_wmb();
  922. }
  923. if (rgid != (gid_t) -1 ||
  924. (egid != (gid_t) -1 && egid != old_rgid))
  925. current->sgid = new_egid;
  926. current->fsgid = new_egid;
  927. current->egid = new_egid;
  928. current->gid = new_rgid;
  929. key_fsgid_changed(current);
  930. proc_id_connector(current, PROC_EVENT_GID);
  931. return 0;
  932. }
  933. /*
  934. * setgid() is implemented like SysV w/ SAVED_IDS
  935. *
  936. * SMP: Same implicit races as above.
  937. */
  938. asmlinkage long sys_setgid(gid_t gid)
  939. {
  940. int old_egid = current->egid;
  941. int retval;
  942. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  943. if (retval)
  944. return retval;
  945. if (capable(CAP_SETGID)) {
  946. if (old_egid != gid) {
  947. set_dumpable(current->mm, suid_dumpable);
  948. smp_wmb();
  949. }
  950. current->gid = current->egid = current->sgid = current->fsgid = gid;
  951. } else if ((gid == current->gid) || (gid == current->sgid)) {
  952. if (old_egid != gid) {
  953. set_dumpable(current->mm, suid_dumpable);
  954. smp_wmb();
  955. }
  956. current->egid = current->fsgid = gid;
  957. }
  958. else
  959. return -EPERM;
  960. key_fsgid_changed(current);
  961. proc_id_connector(current, PROC_EVENT_GID);
  962. return 0;
  963. }
  964. static int set_user(uid_t new_ruid, int dumpclear)
  965. {
  966. struct user_struct *new_user;
  967. new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
  968. if (!new_user)
  969. return -EAGAIN;
  970. if (atomic_read(&new_user->processes) >=
  971. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  972. new_user != current->nsproxy->user_ns->root_user) {
  973. free_uid(new_user);
  974. return -EAGAIN;
  975. }
  976. switch_uid(new_user);
  977. if (dumpclear) {
  978. set_dumpable(current->mm, suid_dumpable);
  979. smp_wmb();
  980. }
  981. current->uid = new_ruid;
  982. return 0;
  983. }
  984. /*
  985. * Unprivileged users may change the real uid to the effective uid
  986. * or vice versa. (BSD-style)
  987. *
  988. * If you set the real uid at all, or set the effective uid to a value not
  989. * equal to the real uid, then the saved uid is set to the new effective uid.
  990. *
  991. * This makes it possible for a setuid program to completely drop its
  992. * privileges, which is often a useful assertion to make when you are doing
  993. * a security audit over a program.
  994. *
  995. * The general idea is that a program which uses just setreuid() will be
  996. * 100% compatible with BSD. A program which uses just setuid() will be
  997. * 100% compatible with POSIX with saved IDs.
  998. */
  999. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  1000. {
  1001. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  1002. int retval;
  1003. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  1004. if (retval)
  1005. return retval;
  1006. new_ruid = old_ruid = current->uid;
  1007. new_euid = old_euid = current->euid;
  1008. old_suid = current->suid;
  1009. if (ruid != (uid_t) -1) {
  1010. new_ruid = ruid;
  1011. if ((old_ruid != ruid) &&
  1012. (current->euid != ruid) &&
  1013. !capable(CAP_SETUID))
  1014. return -EPERM;
  1015. }
  1016. if (euid != (uid_t) -1) {
  1017. new_euid = euid;
  1018. if ((old_ruid != euid) &&
  1019. (current->euid != euid) &&
  1020. (current->suid != euid) &&
  1021. !capable(CAP_SETUID))
  1022. return -EPERM;
  1023. }
  1024. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  1025. return -EAGAIN;
  1026. if (new_euid != old_euid) {
  1027. set_dumpable(current->mm, suid_dumpable);
  1028. smp_wmb();
  1029. }
  1030. current->fsuid = current->euid = new_euid;
  1031. if (ruid != (uid_t) -1 ||
  1032. (euid != (uid_t) -1 && euid != old_ruid))
  1033. current->suid = current->euid;
  1034. current->fsuid = current->euid;
  1035. key_fsuid_changed(current);
  1036. proc_id_connector(current, PROC_EVENT_UID);
  1037. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  1038. }
  1039. /*
  1040. * setuid() is implemented like SysV with SAVED_IDS
  1041. *
  1042. * Note that SAVED_ID's is deficient in that a setuid root program
  1043. * like sendmail, for example, cannot set its uid to be a normal
  1044. * user and then switch back, because if you're root, setuid() sets
  1045. * the saved uid too. If you don't like this, blame the bright people
  1046. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  1047. * will allow a root program to temporarily drop privileges and be able to
  1048. * regain them by swapping the real and effective uid.
  1049. */
  1050. asmlinkage long sys_setuid(uid_t uid)
  1051. {
  1052. int old_euid = current->euid;
  1053. int old_ruid, old_suid, new_suid;
  1054. int retval;
  1055. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  1056. if (retval)
  1057. return retval;
  1058. old_ruid = current->uid;
  1059. old_suid = current->suid;
  1060. new_suid = old_suid;
  1061. if (capable(CAP_SETUID)) {
  1062. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  1063. return -EAGAIN;
  1064. new_suid = uid;
  1065. } else if ((uid != current->uid) && (uid != new_suid))
  1066. return -EPERM;
  1067. if (old_euid != uid) {
  1068. set_dumpable(current->mm, suid_dumpable);
  1069. smp_wmb();
  1070. }
  1071. current->fsuid = current->euid = uid;
  1072. current->suid = new_suid;
  1073. key_fsuid_changed(current);
  1074. proc_id_connector(current, PROC_EVENT_UID);
  1075. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  1076. }
  1077. /*
  1078. * This function implements a generic ability to update ruid, euid,
  1079. * and suid. This allows you to implement the 4.4 compatible seteuid().
  1080. */
  1081. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  1082. {
  1083. int old_ruid = current->uid;
  1084. int old_euid = current->euid;
  1085. int old_suid = current->suid;
  1086. int retval;
  1087. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  1088. if (retval)
  1089. return retval;
  1090. if (!capable(CAP_SETUID)) {
  1091. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  1092. (ruid != current->euid) && (ruid != current->suid))
  1093. return -EPERM;
  1094. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  1095. (euid != current->euid) && (euid != current->suid))
  1096. return -EPERM;
  1097. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  1098. (suid != current->euid) && (suid != current->suid))
  1099. return -EPERM;
  1100. }
  1101. if (ruid != (uid_t) -1) {
  1102. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  1103. return -EAGAIN;
  1104. }
  1105. if (euid != (uid_t) -1) {
  1106. if (euid != current->euid) {
  1107. set_dumpable(current->mm, suid_dumpable);
  1108. smp_wmb();
  1109. }
  1110. current->euid = euid;
  1111. }
  1112. current->fsuid = current->euid;
  1113. if (suid != (uid_t) -1)
  1114. current->suid = suid;
  1115. key_fsuid_changed(current);
  1116. proc_id_connector(current, PROC_EVENT_UID);
  1117. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  1118. }
  1119. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  1120. {
  1121. int retval;
  1122. if (!(retval = put_user(current->uid, ruid)) &&
  1123. !(retval = put_user(current->euid, euid)))
  1124. retval = put_user(current->suid, suid);
  1125. return retval;
  1126. }
  1127. /*
  1128. * Same as above, but for rgid, egid, sgid.
  1129. */
  1130. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  1131. {
  1132. int retval;
  1133. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  1134. if (retval)
  1135. return retval;
  1136. if (!capable(CAP_SETGID)) {
  1137. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  1138. (rgid != current->egid) && (rgid != current->sgid))
  1139. return -EPERM;
  1140. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  1141. (egid != current->egid) && (egid != current->sgid))
  1142. return -EPERM;
  1143. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  1144. (sgid != current->egid) && (sgid != current->sgid))
  1145. return -EPERM;
  1146. }
  1147. if (egid != (gid_t) -1) {
  1148. if (egid != current->egid) {
  1149. set_dumpable(current->mm, suid_dumpable);
  1150. smp_wmb();
  1151. }
  1152. current->egid = egid;
  1153. }
  1154. current->fsgid = current->egid;
  1155. if (rgid != (gid_t) -1)
  1156. current->gid = rgid;
  1157. if (sgid != (gid_t) -1)
  1158. current->sgid = sgid;
  1159. key_fsgid_changed(current);
  1160. proc_id_connector(current, PROC_EVENT_GID);
  1161. return 0;
  1162. }
  1163. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  1164. {
  1165. int retval;
  1166. if (!(retval = put_user(current->gid, rgid)) &&
  1167. !(retval = put_user(current->egid, egid)))
  1168. retval = put_user(current->sgid, sgid);
  1169. return retval;
  1170. }
  1171. /*
  1172. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  1173. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  1174. * whatever uid it wants to). It normally shadows "euid", except when
  1175. * explicitly set by setfsuid() or for access..
  1176. */
  1177. asmlinkage long sys_setfsuid(uid_t uid)
  1178. {
  1179. int old_fsuid;
  1180. old_fsuid = current->fsuid;
  1181. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  1182. return old_fsuid;
  1183. if (uid == current->uid || uid == current->euid ||
  1184. uid == current->suid || uid == current->fsuid ||
  1185. capable(CAP_SETUID)) {
  1186. if (uid != old_fsuid) {
  1187. set_dumpable(current->mm, suid_dumpable);
  1188. smp_wmb();
  1189. }
  1190. current->fsuid = uid;
  1191. }
  1192. key_fsuid_changed(current);
  1193. proc_id_connector(current, PROC_EVENT_UID);
  1194. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1195. return old_fsuid;
  1196. }
  1197. /*
  1198. * Samma på svenska..
  1199. */
  1200. asmlinkage long sys_setfsgid(gid_t gid)
  1201. {
  1202. int old_fsgid;
  1203. old_fsgid = current->fsgid;
  1204. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1205. return old_fsgid;
  1206. if (gid == current->gid || gid == current->egid ||
  1207. gid == current->sgid || gid == current->fsgid ||
  1208. capable(CAP_SETGID)) {
  1209. if (gid != old_fsgid) {
  1210. set_dumpable(current->mm, suid_dumpable);
  1211. smp_wmb();
  1212. }
  1213. current->fsgid = gid;
  1214. key_fsgid_changed(current);
  1215. proc_id_connector(current, PROC_EVENT_GID);
  1216. }
  1217. return old_fsgid;
  1218. }
  1219. asmlinkage long sys_times(struct tms __user * tbuf)
  1220. {
  1221. /*
  1222. * In the SMP world we might just be unlucky and have one of
  1223. * the times increment as we use it. Since the value is an
  1224. * atomically safe type this is just fine. Conceptually its
  1225. * as if the syscall took an instant longer to occur.
  1226. */
  1227. if (tbuf) {
  1228. struct tms tmp;
  1229. struct task_struct *tsk = current;
  1230. struct task_struct *t;
  1231. cputime_t utime, stime, cutime, cstime;
  1232. spin_lock_irq(&tsk->sighand->siglock);
  1233. utime = tsk->signal->utime;
  1234. stime = tsk->signal->stime;
  1235. t = tsk;
  1236. do {
  1237. utime = cputime_add(utime, t->utime);
  1238. stime = cputime_add(stime, t->stime);
  1239. t = next_thread(t);
  1240. } while (t != tsk);
  1241. cutime = tsk->signal->cutime;
  1242. cstime = tsk->signal->cstime;
  1243. spin_unlock_irq(&tsk->sighand->siglock);
  1244. tmp.tms_utime = cputime_to_clock_t(utime);
  1245. tmp.tms_stime = cputime_to_clock_t(stime);
  1246. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1247. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1248. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1249. return -EFAULT;
  1250. }
  1251. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1252. }
  1253. /*
  1254. * This needs some heavy checking ...
  1255. * I just haven't the stomach for it. I also don't fully
  1256. * understand sessions/pgrp etc. Let somebody who does explain it.
  1257. *
  1258. * OK, I think I have the protection semantics right.... this is really
  1259. * only important on a multi-user system anyway, to make sure one user
  1260. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1261. *
  1262. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1263. * LBT 04.03.94
  1264. */
  1265. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1266. {
  1267. struct task_struct *p;
  1268. struct task_struct *group_leader = current->group_leader;
  1269. int err = -EINVAL;
  1270. if (!pid)
  1271. pid = group_leader->pid;
  1272. if (!pgid)
  1273. pgid = pid;
  1274. if (pgid < 0)
  1275. return -EINVAL;
  1276. /* From this point forward we keep holding onto the tasklist lock
  1277. * so that our parent does not change from under us. -DaveM
  1278. */
  1279. write_lock_irq(&tasklist_lock);
  1280. err = -ESRCH;
  1281. p = find_task_by_pid(pid);
  1282. if (!p)
  1283. goto out;
  1284. err = -EINVAL;
  1285. if (!thread_group_leader(p))
  1286. goto out;
  1287. if (p->real_parent->tgid == group_leader->tgid) {
  1288. err = -EPERM;
  1289. if (task_session(p) != task_session(group_leader))
  1290. goto out;
  1291. err = -EACCES;
  1292. if (p->did_exec)
  1293. goto out;
  1294. } else {
  1295. err = -ESRCH;
  1296. if (p != group_leader)
  1297. goto out;
  1298. }
  1299. err = -EPERM;
  1300. if (p->signal->leader)
  1301. goto out;
  1302. if (pgid != pid) {
  1303. struct task_struct *g =
  1304. find_task_by_pid_type(PIDTYPE_PGID, pgid);
  1305. if (!g || task_session(g) != task_session(group_leader))
  1306. goto out;
  1307. }
  1308. err = security_task_setpgid(p, pgid);
  1309. if (err)
  1310. goto out;
  1311. if (process_group(p) != pgid) {
  1312. detach_pid(p, PIDTYPE_PGID);
  1313. p->signal->pgrp = pgid;
  1314. attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
  1315. }
  1316. err = 0;
  1317. out:
  1318. /* All paths lead to here, thus we are safe. -DaveM */
  1319. write_unlock_irq(&tasklist_lock);
  1320. return err;
  1321. }
  1322. asmlinkage long sys_getpgid(pid_t pid)
  1323. {
  1324. if (!pid)
  1325. return process_group(current);
  1326. else {
  1327. int retval;
  1328. struct task_struct *p;
  1329. read_lock(&tasklist_lock);
  1330. p = find_task_by_pid(pid);
  1331. retval = -ESRCH;
  1332. if (p) {
  1333. retval = security_task_getpgid(p);
  1334. if (!retval)
  1335. retval = process_group(p);
  1336. }
  1337. read_unlock(&tasklist_lock);
  1338. return retval;
  1339. }
  1340. }
  1341. #ifdef __ARCH_WANT_SYS_GETPGRP
  1342. asmlinkage long sys_getpgrp(void)
  1343. {
  1344. /* SMP - assuming writes are word atomic this is fine */
  1345. return process_group(current);
  1346. }
  1347. #endif
  1348. asmlinkage long sys_getsid(pid_t pid)
  1349. {
  1350. if (!pid)
  1351. return process_session(current);
  1352. else {
  1353. int retval;
  1354. struct task_struct *p;
  1355. read_lock(&tasklist_lock);
  1356. p = find_task_by_pid(pid);
  1357. retval = -ESRCH;
  1358. if (p) {
  1359. retval = security_task_getsid(p);
  1360. if (!retval)
  1361. retval = process_session(p);
  1362. }
  1363. read_unlock(&tasklist_lock);
  1364. return retval;
  1365. }
  1366. }
  1367. asmlinkage long sys_setsid(void)
  1368. {
  1369. struct task_struct *group_leader = current->group_leader;
  1370. pid_t session;
  1371. int err = -EPERM;
  1372. write_lock_irq(&tasklist_lock);
  1373. /* Fail if I am already a session leader */
  1374. if (group_leader->signal->leader)
  1375. goto out;
  1376. session = group_leader->pid;
  1377. /* Fail if a process group id already exists that equals the
  1378. * proposed session id.
  1379. *
  1380. * Don't check if session id == 1 because kernel threads use this
  1381. * session id and so the check will always fail and make it so
  1382. * init cannot successfully call setsid.
  1383. */
  1384. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1385. goto out;
  1386. group_leader->signal->leader = 1;
  1387. __set_special_pids(session, session);
  1388. spin_lock(&group_leader->sighand->siglock);
  1389. group_leader->signal->tty = NULL;
  1390. spin_unlock(&group_leader->sighand->siglock);
  1391. err = process_group(group_leader);
  1392. out:
  1393. write_unlock_irq(&tasklist_lock);
  1394. return err;
  1395. }
  1396. /*
  1397. * Supplementary group IDs
  1398. */
  1399. /* init to 2 - one for init_task, one to ensure it is never freed */
  1400. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1401. struct group_info *groups_alloc(int gidsetsize)
  1402. {
  1403. struct group_info *group_info;
  1404. int nblocks;
  1405. int i;
  1406. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1407. /* Make sure we always allocate at least one indirect block pointer */
  1408. nblocks = nblocks ? : 1;
  1409. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1410. if (!group_info)
  1411. return NULL;
  1412. group_info->ngroups = gidsetsize;
  1413. group_info->nblocks = nblocks;
  1414. atomic_set(&group_info->usage, 1);
  1415. if (gidsetsize <= NGROUPS_SMALL)
  1416. group_info->blocks[0] = group_info->small_block;
  1417. else {
  1418. for (i = 0; i < nblocks; i++) {
  1419. gid_t *b;
  1420. b = (void *)__get_free_page(GFP_USER);
  1421. if (!b)
  1422. goto out_undo_partial_alloc;
  1423. group_info->blocks[i] = b;
  1424. }
  1425. }
  1426. return group_info;
  1427. out_undo_partial_alloc:
  1428. while (--i >= 0) {
  1429. free_page((unsigned long)group_info->blocks[i]);
  1430. }
  1431. kfree(group_info);
  1432. return NULL;
  1433. }
  1434. EXPORT_SYMBOL(groups_alloc);
  1435. void groups_free(struct group_info *group_info)
  1436. {
  1437. if (group_info->blocks[0] != group_info->small_block) {
  1438. int i;
  1439. for (i = 0; i < group_info->nblocks; i++)
  1440. free_page((unsigned long)group_info->blocks[i]);
  1441. }
  1442. kfree(group_info);
  1443. }
  1444. EXPORT_SYMBOL(groups_free);
  1445. /* export the group_info to a user-space array */
  1446. static int groups_to_user(gid_t __user *grouplist,
  1447. struct group_info *group_info)
  1448. {
  1449. int i;
  1450. int count = group_info->ngroups;
  1451. for (i = 0; i < group_info->nblocks; i++) {
  1452. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1453. int off = i * NGROUPS_PER_BLOCK;
  1454. int len = cp_count * sizeof(*grouplist);
  1455. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1456. return -EFAULT;
  1457. count -= cp_count;
  1458. }
  1459. return 0;
  1460. }
  1461. /* fill a group_info from a user-space array - it must be allocated already */
  1462. static int groups_from_user(struct group_info *group_info,
  1463. gid_t __user *grouplist)
  1464. {
  1465. int i;
  1466. int count = group_info->ngroups;
  1467. for (i = 0; i < group_info->nblocks; i++) {
  1468. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1469. int off = i * NGROUPS_PER_BLOCK;
  1470. int len = cp_count * sizeof(*grouplist);
  1471. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1472. return -EFAULT;
  1473. count -= cp_count;
  1474. }
  1475. return 0;
  1476. }
  1477. /* a simple Shell sort */
  1478. static void groups_sort(struct group_info *group_info)
  1479. {
  1480. int base, max, stride;
  1481. int gidsetsize = group_info->ngroups;
  1482. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1483. ; /* nothing */
  1484. stride /= 3;
  1485. while (stride) {
  1486. max = gidsetsize - stride;
  1487. for (base = 0; base < max; base++) {
  1488. int left = base;
  1489. int right = left + stride;
  1490. gid_t tmp = GROUP_AT(group_info, right);
  1491. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1492. GROUP_AT(group_info, right) =
  1493. GROUP_AT(group_info, left);
  1494. right = left;
  1495. left -= stride;
  1496. }
  1497. GROUP_AT(group_info, right) = tmp;
  1498. }
  1499. stride /= 3;
  1500. }
  1501. }
  1502. /* a simple bsearch */
  1503. int groups_search(struct group_info *group_info, gid_t grp)
  1504. {
  1505. unsigned int left, right;
  1506. if (!group_info)
  1507. return 0;
  1508. left = 0;
  1509. right = group_info->ngroups;
  1510. while (left < right) {
  1511. unsigned int mid = (left+right)/2;
  1512. int cmp = grp - GROUP_AT(group_info, mid);
  1513. if (cmp > 0)
  1514. left = mid + 1;
  1515. else if (cmp < 0)
  1516. right = mid;
  1517. else
  1518. return 1;
  1519. }
  1520. return 0;
  1521. }
  1522. /* validate and set current->group_info */
  1523. int set_current_groups(struct group_info *group_info)
  1524. {
  1525. int retval;
  1526. struct group_info *old_info;
  1527. retval = security_task_setgroups(group_info);
  1528. if (retval)
  1529. return retval;
  1530. groups_sort(group_info);
  1531. get_group_info(group_info);
  1532. task_lock(current);
  1533. old_info = current->group_info;
  1534. current->group_info = group_info;
  1535. task_unlock(current);
  1536. put_group_info(old_info);
  1537. return 0;
  1538. }
  1539. EXPORT_SYMBOL(set_current_groups);
  1540. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1541. {
  1542. int i = 0;
  1543. /*
  1544. * SMP: Nobody else can change our grouplist. Thus we are
  1545. * safe.
  1546. */
  1547. if (gidsetsize < 0)
  1548. return -EINVAL;
  1549. /* no need to grab task_lock here; it cannot change */
  1550. i = current->group_info->ngroups;
  1551. if (gidsetsize) {
  1552. if (i > gidsetsize) {
  1553. i = -EINVAL;
  1554. goto out;
  1555. }
  1556. if (groups_to_user(grouplist, current->group_info)) {
  1557. i = -EFAULT;
  1558. goto out;
  1559. }
  1560. }
  1561. out:
  1562. return i;
  1563. }
  1564. /*
  1565. * SMP: Our groups are copy-on-write. We can set them safely
  1566. * without another task interfering.
  1567. */
  1568. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1569. {
  1570. struct group_info *group_info;
  1571. int retval;
  1572. if (!capable(CAP_SETGID))
  1573. return -EPERM;
  1574. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1575. return -EINVAL;
  1576. group_info = groups_alloc(gidsetsize);
  1577. if (!group_info)
  1578. return -ENOMEM;
  1579. retval = groups_from_user(group_info, grouplist);
  1580. if (retval) {
  1581. put_group_info(group_info);
  1582. return retval;
  1583. }
  1584. retval = set_current_groups(group_info);
  1585. put_group_info(group_info);
  1586. return retval;
  1587. }
  1588. /*
  1589. * Check whether we're fsgid/egid or in the supplemental group..
  1590. */
  1591. int in_group_p(gid_t grp)
  1592. {
  1593. int retval = 1;
  1594. if (grp != current->fsgid)
  1595. retval = groups_search(current->group_info, grp);
  1596. return retval;
  1597. }
  1598. EXPORT_SYMBOL(in_group_p);
  1599. int in_egroup_p(gid_t grp)
  1600. {
  1601. int retval = 1;
  1602. if (grp != current->egid)
  1603. retval = groups_search(current->group_info, grp);
  1604. return retval;
  1605. }
  1606. EXPORT_SYMBOL(in_egroup_p);
  1607. DECLARE_RWSEM(uts_sem);
  1608. EXPORT_SYMBOL(uts_sem);
  1609. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1610. {
  1611. int errno = 0;
  1612. down_read(&uts_sem);
  1613. if (copy_to_user(name, utsname(), sizeof *name))
  1614. errno = -EFAULT;
  1615. up_read(&uts_sem);
  1616. return errno;
  1617. }
  1618. asmlinkage long sys_sethostname(char __user *name, int len)
  1619. {
  1620. int errno;
  1621. char tmp[__NEW_UTS_LEN];
  1622. if (!capable(CAP_SYS_ADMIN))
  1623. return -EPERM;
  1624. if (len < 0 || len > __NEW_UTS_LEN)
  1625. return -EINVAL;
  1626. down_write(&uts_sem);
  1627. errno = -EFAULT;
  1628. if (!copy_from_user(tmp, name, len)) {
  1629. memcpy(utsname()->nodename, tmp, len);
  1630. utsname()->nodename[len] = 0;
  1631. errno = 0;
  1632. }
  1633. up_write(&uts_sem);
  1634. return errno;
  1635. }
  1636. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1637. asmlinkage long sys_gethostname(char __user *name, int len)
  1638. {
  1639. int i, errno;
  1640. if (len < 0)
  1641. return -EINVAL;
  1642. down_read(&uts_sem);
  1643. i = 1 + strlen(utsname()->nodename);
  1644. if (i > len)
  1645. i = len;
  1646. errno = 0;
  1647. if (copy_to_user(name, utsname()->nodename, i))
  1648. errno = -EFAULT;
  1649. up_read(&uts_sem);
  1650. return errno;
  1651. }
  1652. #endif
  1653. /*
  1654. * Only setdomainname; getdomainname can be implemented by calling
  1655. * uname()
  1656. */
  1657. asmlinkage long sys_setdomainname(char __user *name, int len)
  1658. {
  1659. int errno;
  1660. char tmp[__NEW_UTS_LEN];
  1661. if (!capable(CAP_SYS_ADMIN))
  1662. return -EPERM;
  1663. if (len < 0 || len > __NEW_UTS_LEN)
  1664. return -EINVAL;
  1665. down_write(&uts_sem);
  1666. errno = -EFAULT;
  1667. if (!copy_from_user(tmp, name, len)) {
  1668. memcpy(utsname()->domainname, tmp, len);
  1669. utsname()->domainname[len] = 0;
  1670. errno = 0;
  1671. }
  1672. up_write(&uts_sem);
  1673. return errno;
  1674. }
  1675. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1676. {
  1677. if (resource >= RLIM_NLIMITS)
  1678. return -EINVAL;
  1679. else {
  1680. struct rlimit value;
  1681. task_lock(current->group_leader);
  1682. value = current->signal->rlim[resource];
  1683. task_unlock(current->group_leader);
  1684. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1685. }
  1686. }
  1687. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1688. /*
  1689. * Back compatibility for getrlimit. Needed for some apps.
  1690. */
  1691. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1692. {
  1693. struct rlimit x;
  1694. if (resource >= RLIM_NLIMITS)
  1695. return -EINVAL;
  1696. task_lock(current->group_leader);
  1697. x = current->signal->rlim[resource];
  1698. task_unlock(current->group_leader);
  1699. if (x.rlim_cur > 0x7FFFFFFF)
  1700. x.rlim_cur = 0x7FFFFFFF;
  1701. if (x.rlim_max > 0x7FFFFFFF)
  1702. x.rlim_max = 0x7FFFFFFF;
  1703. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1704. }
  1705. #endif
  1706. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1707. {
  1708. struct rlimit new_rlim, *old_rlim;
  1709. unsigned long it_prof_secs;
  1710. int retval;
  1711. if (resource >= RLIM_NLIMITS)
  1712. return -EINVAL;
  1713. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1714. return -EFAULT;
  1715. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1716. return -EINVAL;
  1717. old_rlim = current->signal->rlim + resource;
  1718. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1719. !capable(CAP_SYS_RESOURCE))
  1720. return -EPERM;
  1721. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1722. return -EPERM;
  1723. retval = security_task_setrlimit(resource, &new_rlim);
  1724. if (retval)
  1725. return retval;
  1726. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1727. /*
  1728. * The caller is asking for an immediate RLIMIT_CPU
  1729. * expiry. But we use the zero value to mean "it was
  1730. * never set". So let's cheat and make it one second
  1731. * instead
  1732. */
  1733. new_rlim.rlim_cur = 1;
  1734. }
  1735. task_lock(current->group_leader);
  1736. *old_rlim = new_rlim;
  1737. task_unlock(current->group_leader);
  1738. if (resource != RLIMIT_CPU)
  1739. goto out;
  1740. /*
  1741. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1742. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1743. * very long-standing error, and fixing it now risks breakage of
  1744. * applications, so we live with it
  1745. */
  1746. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1747. goto out;
  1748. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1749. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1750. unsigned long rlim_cur = new_rlim.rlim_cur;
  1751. cputime_t cputime;
  1752. cputime = secs_to_cputime(rlim_cur);
  1753. read_lock(&tasklist_lock);
  1754. spin_lock_irq(&current->sighand->siglock);
  1755. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1756. spin_unlock_irq(&current->sighand->siglock);
  1757. read_unlock(&tasklist_lock);
  1758. }
  1759. out:
  1760. return 0;
  1761. }
  1762. /*
  1763. * It would make sense to put struct rusage in the task_struct,
  1764. * except that would make the task_struct be *really big*. After
  1765. * task_struct gets moved into malloc'ed memory, it would
  1766. * make sense to do this. It will make moving the rest of the information
  1767. * a lot simpler! (Which we're not doing right now because we're not
  1768. * measuring them yet).
  1769. *
  1770. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1771. * races with threads incrementing their own counters. But since word
  1772. * reads are atomic, we either get new values or old values and we don't
  1773. * care which for the sums. We always take the siglock to protect reading
  1774. * the c* fields from p->signal from races with exit.c updating those
  1775. * fields when reaping, so a sample either gets all the additions of a
  1776. * given child after it's reaped, or none so this sample is before reaping.
  1777. *
  1778. * Locking:
  1779. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1780. * for the cases current multithreaded, non-current single threaded
  1781. * non-current multithreaded. Thread traversal is now safe with
  1782. * the siglock held.
  1783. * Strictly speaking, we donot need to take the siglock if we are current and
  1784. * single threaded, as no one else can take our signal_struct away, no one
  1785. * else can reap the children to update signal->c* counters, and no one else
  1786. * can race with the signal-> fields. If we do not take any lock, the
  1787. * signal-> fields could be read out of order while another thread was just
  1788. * exiting. So we should place a read memory barrier when we avoid the lock.
  1789. * On the writer side, write memory barrier is implied in __exit_signal
  1790. * as __exit_signal releases the siglock spinlock after updating the signal->
  1791. * fields. But we don't do this yet to keep things simple.
  1792. *
  1793. */
  1794. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1795. {
  1796. struct task_struct *t;
  1797. unsigned long flags;
  1798. cputime_t utime, stime;
  1799. memset((char *) r, 0, sizeof *r);
  1800. utime = stime = cputime_zero;
  1801. rcu_read_lock();
  1802. if (!lock_task_sighand(p, &flags)) {
  1803. rcu_read_unlock();
  1804. return;
  1805. }
  1806. switch (who) {
  1807. case RUSAGE_BOTH:
  1808. case RUSAGE_CHILDREN:
  1809. utime = p->signal->cutime;
  1810. stime = p->signal->cstime;
  1811. r->ru_nvcsw = p->signal->cnvcsw;
  1812. r->ru_nivcsw = p->signal->cnivcsw;
  1813. r->ru_minflt = p->signal->cmin_flt;
  1814. r->ru_majflt = p->signal->cmaj_flt;
  1815. r->ru_inblock = p->signal->cinblock;
  1816. r->ru_oublock = p->signal->coublock;
  1817. if (who == RUSAGE_CHILDREN)
  1818. break;
  1819. case RUSAGE_SELF:
  1820. utime = cputime_add(utime, p->signal->utime);
  1821. stime = cputime_add(stime, p->signal->stime);
  1822. r->ru_nvcsw += p->signal->nvcsw;
  1823. r->ru_nivcsw += p->signal->nivcsw;
  1824. r->ru_minflt += p->signal->min_flt;
  1825. r->ru_majflt += p->signal->maj_flt;
  1826. r->ru_inblock += p->signal->inblock;
  1827. r->ru_oublock += p->signal->oublock;
  1828. t = p;
  1829. do {
  1830. utime = cputime_add(utime, t->utime);
  1831. stime = cputime_add(stime, t->stime);
  1832. r->ru_nvcsw += t->nvcsw;
  1833. r->ru_nivcsw += t->nivcsw;
  1834. r->ru_minflt += t->min_flt;
  1835. r->ru_majflt += t->maj_flt;
  1836. r->ru_inblock += task_io_get_inblock(t);
  1837. r->ru_oublock += task_io_get_oublock(t);
  1838. t = next_thread(t);
  1839. } while (t != p);
  1840. break;
  1841. default:
  1842. BUG();
  1843. }
  1844. unlock_task_sighand(p, &flags);
  1845. rcu_read_unlock();
  1846. cputime_to_timeval(utime, &r->ru_utime);
  1847. cputime_to_timeval(stime, &r->ru_stime);
  1848. }
  1849. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1850. {
  1851. struct rusage r;
  1852. k_getrusage(p, who, &r);
  1853. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1854. }
  1855. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1856. {
  1857. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1858. return -EINVAL;
  1859. return getrusage(current, who, ru);
  1860. }
  1861. asmlinkage long sys_umask(int mask)
  1862. {
  1863. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1864. return mask;
  1865. }
  1866. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1867. unsigned long arg4, unsigned long arg5)
  1868. {
  1869. long error;
  1870. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1871. if (error)
  1872. return error;
  1873. switch (option) {
  1874. case PR_SET_PDEATHSIG:
  1875. if (!valid_signal(arg2)) {
  1876. error = -EINVAL;
  1877. break;
  1878. }
  1879. current->pdeath_signal = arg2;
  1880. break;
  1881. case PR_GET_PDEATHSIG:
  1882. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1883. break;
  1884. case PR_GET_DUMPABLE:
  1885. error = get_dumpable(current->mm);
  1886. break;
  1887. case PR_SET_DUMPABLE:
  1888. if (arg2 < 0 || arg2 > 1) {
  1889. error = -EINVAL;
  1890. break;
  1891. }
  1892. set_dumpable(current->mm, arg2);
  1893. break;
  1894. case PR_SET_UNALIGN:
  1895. error = SET_UNALIGN_CTL(current, arg2);
  1896. break;
  1897. case PR_GET_UNALIGN:
  1898. error = GET_UNALIGN_CTL(current, arg2);
  1899. break;
  1900. case PR_SET_FPEMU:
  1901. error = SET_FPEMU_CTL(current, arg2);
  1902. break;
  1903. case PR_GET_FPEMU:
  1904. error = GET_FPEMU_CTL(current, arg2);
  1905. break;
  1906. case PR_SET_FPEXC:
  1907. error = SET_FPEXC_CTL(current, arg2);
  1908. break;
  1909. case PR_GET_FPEXC:
  1910. error = GET_FPEXC_CTL(current, arg2);
  1911. break;
  1912. case PR_GET_TIMING:
  1913. error = PR_TIMING_STATISTICAL;
  1914. break;
  1915. case PR_SET_TIMING:
  1916. if (arg2 == PR_TIMING_STATISTICAL)
  1917. error = 0;
  1918. else
  1919. error = -EINVAL;
  1920. break;
  1921. case PR_GET_KEEPCAPS:
  1922. if (current->keep_capabilities)
  1923. error = 1;
  1924. break;
  1925. case PR_SET_KEEPCAPS:
  1926. if (arg2 != 0 && arg2 != 1) {
  1927. error = -EINVAL;
  1928. break;
  1929. }
  1930. current->keep_capabilities = arg2;
  1931. break;
  1932. case PR_SET_NAME: {
  1933. struct task_struct *me = current;
  1934. unsigned char ncomm[sizeof(me->comm)];
  1935. ncomm[sizeof(me->comm)-1] = 0;
  1936. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1937. sizeof(me->comm)-1) < 0)
  1938. return -EFAULT;
  1939. set_task_comm(me, ncomm);
  1940. return 0;
  1941. }
  1942. case PR_GET_NAME: {
  1943. struct task_struct *me = current;
  1944. unsigned char tcomm[sizeof(me->comm)];
  1945. get_task_comm(tcomm, me);
  1946. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1947. return -EFAULT;
  1948. return 0;
  1949. }
  1950. case PR_GET_ENDIAN:
  1951. error = GET_ENDIAN(current, arg2);
  1952. break;
  1953. case PR_SET_ENDIAN:
  1954. error = SET_ENDIAN(current, arg2);
  1955. break;
  1956. case PR_GET_SECCOMP:
  1957. error = prctl_get_seccomp();
  1958. break;
  1959. case PR_SET_SECCOMP:
  1960. error = prctl_set_seccomp(arg2);
  1961. break;
  1962. default:
  1963. error = -EINVAL;
  1964. break;
  1965. }
  1966. return error;
  1967. }
  1968. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1969. struct getcpu_cache __user *cache)
  1970. {
  1971. int err = 0;
  1972. int cpu = raw_smp_processor_id();
  1973. if (cpup)
  1974. err |= put_user(cpu, cpup);
  1975. if (nodep)
  1976. err |= put_user(cpu_to_node(cpu), nodep);
  1977. if (cache) {
  1978. /*
  1979. * The cache is not needed for this implementation,
  1980. * but make sure user programs pass something
  1981. * valid. vsyscall implementations can instead make
  1982. * good use of the cache. Only use t0 and t1 because
  1983. * these are available in both 32bit and 64bit ABI (no
  1984. * need for a compat_getcpu). 32bit has enough
  1985. * padding
  1986. */
  1987. unsigned long t0, t1;
  1988. get_user(t0, &cache->blob[0]);
  1989. get_user(t1, &cache->blob[1]);
  1990. t0++;
  1991. t1++;
  1992. put_user(t0, &cache->blob[0]);
  1993. put_user(t1, &cache->blob[1]);
  1994. }
  1995. return err ? -EFAULT : 0;
  1996. }
  1997. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1998. static void argv_cleanup(char **argv, char **envp)
  1999. {
  2000. argv_free(argv);
  2001. }
  2002. /**
  2003. * orderly_poweroff - Trigger an orderly system poweroff
  2004. * @force: force poweroff if command execution fails
  2005. *
  2006. * This may be called from any context to trigger a system shutdown.
  2007. * If the orderly shutdown fails, it will force an immediate shutdown.
  2008. */
  2009. int orderly_poweroff(bool force)
  2010. {
  2011. int argc;
  2012. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  2013. static char *envp[] = {
  2014. "HOME=/",
  2015. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  2016. NULL
  2017. };
  2018. int ret = -ENOMEM;
  2019. struct subprocess_info *info;
  2020. if (argv == NULL) {
  2021. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  2022. __func__, poweroff_cmd);
  2023. goto out;
  2024. }
  2025. info = call_usermodehelper_setup(argv[0], argv, envp);
  2026. if (info == NULL) {
  2027. argv_free(argv);
  2028. goto out;
  2029. }
  2030. call_usermodehelper_setcleanup(info, argv_cleanup);
  2031. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  2032. out:
  2033. if (ret && force) {
  2034. printk(KERN_WARNING "Failed to start orderly shutdown: "
  2035. "forcing the issue\n");
  2036. /* I guess this should try to kick off some daemon to
  2037. sync and poweroff asap. Or not even bother syncing
  2038. if we're doing an emergency shutdown? */
  2039. emergency_sync();
  2040. kernel_power_off();
  2041. }
  2042. return ret;
  2043. }
  2044. EXPORT_SYMBOL_GPL(orderly_poweroff);