sys.c 56 KB

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