sys.c 56 KB

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