sys.c 58 KB

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