sys.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355
  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. }
  708. /**
  709. * kernel_restart - reboot the system
  710. * @cmd: pointer to buffer containing command to execute for restart
  711. * or %NULL
  712. *
  713. * Shutdown everything and perform a clean reboot.
  714. * This is not safe to call in interrupt context.
  715. */
  716. void kernel_restart(char *cmd)
  717. {
  718. kernel_restart_prepare(cmd);
  719. if (!cmd)
  720. printk(KERN_EMERG "Restarting system.\n");
  721. else
  722. printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
  723. machine_restart(cmd);
  724. }
  725. EXPORT_SYMBOL_GPL(kernel_restart);
  726. /**
  727. * kernel_kexec - reboot the system
  728. *
  729. * Move into place and start executing a preloaded standalone
  730. * executable. If nothing was preloaded return an error.
  731. */
  732. static void kernel_kexec(void)
  733. {
  734. #ifdef CONFIG_KEXEC
  735. struct kimage *image;
  736. image = xchg(&kexec_image, NULL);
  737. if (!image)
  738. return;
  739. kernel_restart_prepare(NULL);
  740. printk(KERN_EMERG "Starting new kernel\n");
  741. machine_shutdown();
  742. machine_kexec(image);
  743. #endif
  744. }
  745. void kernel_shutdown_prepare(enum system_states state)
  746. {
  747. blocking_notifier_call_chain(&reboot_notifier_list,
  748. (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
  749. system_state = state;
  750. device_shutdown();
  751. }
  752. /**
  753. * kernel_halt - halt the system
  754. *
  755. * Shutdown everything and perform a clean system halt.
  756. */
  757. void kernel_halt(void)
  758. {
  759. kernel_shutdown_prepare(SYSTEM_HALT);
  760. printk(KERN_EMERG "System halted.\n");
  761. machine_halt();
  762. }
  763. EXPORT_SYMBOL_GPL(kernel_halt);
  764. /**
  765. * kernel_power_off - power_off the system
  766. *
  767. * Shutdown everything and perform a clean system power_off.
  768. */
  769. void kernel_power_off(void)
  770. {
  771. kernel_shutdown_prepare(SYSTEM_POWER_OFF);
  772. if (pm_power_off_prepare)
  773. pm_power_off_prepare();
  774. printk(KERN_EMERG "Power down.\n");
  775. machine_power_off();
  776. }
  777. EXPORT_SYMBOL_GPL(kernel_power_off);
  778. /*
  779. * Reboot system call: for obvious reasons only root may call it,
  780. * and even root needs to set up some magic numbers in the registers
  781. * so that some mistake won't make this reboot the whole machine.
  782. * You can also set the meaning of the ctrl-alt-del-key here.
  783. *
  784. * reboot doesn't sync: do that yourself before calling this.
  785. */
  786. asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
  787. {
  788. char buffer[256];
  789. /* We only trust the superuser with rebooting the system. */
  790. if (!capable(CAP_SYS_BOOT))
  791. return -EPERM;
  792. /* For safety, we require "magic" arguments. */
  793. if (magic1 != LINUX_REBOOT_MAGIC1 ||
  794. (magic2 != LINUX_REBOOT_MAGIC2 &&
  795. magic2 != LINUX_REBOOT_MAGIC2A &&
  796. magic2 != LINUX_REBOOT_MAGIC2B &&
  797. magic2 != LINUX_REBOOT_MAGIC2C))
  798. return -EINVAL;
  799. /* Instead of trying to make the power_off code look like
  800. * halt when pm_power_off is not set do it the easy way.
  801. */
  802. if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
  803. cmd = LINUX_REBOOT_CMD_HALT;
  804. lock_kernel();
  805. switch (cmd) {
  806. case LINUX_REBOOT_CMD_RESTART:
  807. kernel_restart(NULL);
  808. break;
  809. case LINUX_REBOOT_CMD_CAD_ON:
  810. C_A_D = 1;
  811. break;
  812. case LINUX_REBOOT_CMD_CAD_OFF:
  813. C_A_D = 0;
  814. break;
  815. case LINUX_REBOOT_CMD_HALT:
  816. kernel_halt();
  817. unlock_kernel();
  818. do_exit(0);
  819. break;
  820. case LINUX_REBOOT_CMD_POWER_OFF:
  821. kernel_power_off();
  822. unlock_kernel();
  823. do_exit(0);
  824. break;
  825. case LINUX_REBOOT_CMD_RESTART2:
  826. if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
  827. unlock_kernel();
  828. return -EFAULT;
  829. }
  830. buffer[sizeof(buffer) - 1] = '\0';
  831. kernel_restart(buffer);
  832. break;
  833. case LINUX_REBOOT_CMD_KEXEC:
  834. kernel_kexec();
  835. unlock_kernel();
  836. return -EINVAL;
  837. #ifdef CONFIG_SOFTWARE_SUSPEND
  838. case LINUX_REBOOT_CMD_SW_SUSPEND:
  839. {
  840. int ret = hibernate();
  841. unlock_kernel();
  842. return ret;
  843. }
  844. #endif
  845. default:
  846. unlock_kernel();
  847. return -EINVAL;
  848. }
  849. unlock_kernel();
  850. return 0;
  851. }
  852. static void deferred_cad(struct work_struct *dummy)
  853. {
  854. kernel_restart(NULL);
  855. }
  856. /*
  857. * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
  858. * As it's called within an interrupt, it may NOT sync: the only choice
  859. * is whether to reboot at once, or just ignore the ctrl-alt-del.
  860. */
  861. void ctrl_alt_del(void)
  862. {
  863. static DECLARE_WORK(cad_work, deferred_cad);
  864. if (C_A_D)
  865. schedule_work(&cad_work);
  866. else
  867. kill_cad_pid(SIGINT, 1);
  868. }
  869. /*
  870. * Unprivileged users may change the real gid to the effective gid
  871. * or vice versa. (BSD-style)
  872. *
  873. * If you set the real gid at all, or set the effective gid to a value not
  874. * equal to the real gid, then the saved gid is set to the new effective gid.
  875. *
  876. * This makes it possible for a setgid program to completely drop its
  877. * privileges, which is often a useful assertion to make when you are doing
  878. * a security audit over a program.
  879. *
  880. * The general idea is that a program which uses just setregid() will be
  881. * 100% compatible with BSD. A program which uses just setgid() will be
  882. * 100% compatible with POSIX with saved IDs.
  883. *
  884. * SMP: There are not races, the GIDs are checked only by filesystem
  885. * operations (as far as semantic preservation is concerned).
  886. */
  887. asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
  888. {
  889. int old_rgid = current->gid;
  890. int old_egid = current->egid;
  891. int new_rgid = old_rgid;
  892. int new_egid = old_egid;
  893. int retval;
  894. retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
  895. if (retval)
  896. return retval;
  897. if (rgid != (gid_t) -1) {
  898. if ((old_rgid == rgid) ||
  899. (current->egid==rgid) ||
  900. capable(CAP_SETGID))
  901. new_rgid = rgid;
  902. else
  903. return -EPERM;
  904. }
  905. if (egid != (gid_t) -1) {
  906. if ((old_rgid == egid) ||
  907. (current->egid == egid) ||
  908. (current->sgid == egid) ||
  909. capable(CAP_SETGID))
  910. new_egid = egid;
  911. else
  912. return -EPERM;
  913. }
  914. if (new_egid != old_egid) {
  915. set_dumpable(current->mm, suid_dumpable);
  916. smp_wmb();
  917. }
  918. if (rgid != (gid_t) -1 ||
  919. (egid != (gid_t) -1 && egid != old_rgid))
  920. current->sgid = new_egid;
  921. current->fsgid = new_egid;
  922. current->egid = new_egid;
  923. current->gid = new_rgid;
  924. key_fsgid_changed(current);
  925. proc_id_connector(current, PROC_EVENT_GID);
  926. return 0;
  927. }
  928. /*
  929. * setgid() is implemented like SysV w/ SAVED_IDS
  930. *
  931. * SMP: Same implicit races as above.
  932. */
  933. asmlinkage long sys_setgid(gid_t gid)
  934. {
  935. int old_egid = current->egid;
  936. int retval;
  937. retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
  938. if (retval)
  939. return retval;
  940. if (capable(CAP_SETGID)) {
  941. if (old_egid != gid) {
  942. set_dumpable(current->mm, suid_dumpable);
  943. smp_wmb();
  944. }
  945. current->gid = current->egid = current->sgid = current->fsgid = gid;
  946. } else if ((gid == current->gid) || (gid == current->sgid)) {
  947. if (old_egid != gid) {
  948. set_dumpable(current->mm, suid_dumpable);
  949. smp_wmb();
  950. }
  951. current->egid = current->fsgid = gid;
  952. }
  953. else
  954. return -EPERM;
  955. key_fsgid_changed(current);
  956. proc_id_connector(current, PROC_EVENT_GID);
  957. return 0;
  958. }
  959. static int set_user(uid_t new_ruid, int dumpclear)
  960. {
  961. struct user_struct *new_user;
  962. new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
  963. if (!new_user)
  964. return -EAGAIN;
  965. if (atomic_read(&new_user->processes) >=
  966. current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
  967. new_user != current->nsproxy->user_ns->root_user) {
  968. free_uid(new_user);
  969. return -EAGAIN;
  970. }
  971. switch_uid(new_user);
  972. if (dumpclear) {
  973. set_dumpable(current->mm, suid_dumpable);
  974. smp_wmb();
  975. }
  976. current->uid = new_ruid;
  977. return 0;
  978. }
  979. /*
  980. * Unprivileged users may change the real uid to the effective uid
  981. * or vice versa. (BSD-style)
  982. *
  983. * If you set the real uid at all, or set the effective uid to a value not
  984. * equal to the real uid, then the saved uid is set to the new effective uid.
  985. *
  986. * This makes it possible for a setuid program to completely drop its
  987. * privileges, which is often a useful assertion to make when you are doing
  988. * a security audit over a program.
  989. *
  990. * The general idea is that a program which uses just setreuid() will be
  991. * 100% compatible with BSD. A program which uses just setuid() will be
  992. * 100% compatible with POSIX with saved IDs.
  993. */
  994. asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
  995. {
  996. int old_ruid, old_euid, old_suid, new_ruid, new_euid;
  997. int retval;
  998. retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
  999. if (retval)
  1000. return retval;
  1001. new_ruid = old_ruid = current->uid;
  1002. new_euid = old_euid = current->euid;
  1003. old_suid = current->suid;
  1004. if (ruid != (uid_t) -1) {
  1005. new_ruid = ruid;
  1006. if ((old_ruid != ruid) &&
  1007. (current->euid != ruid) &&
  1008. !capable(CAP_SETUID))
  1009. return -EPERM;
  1010. }
  1011. if (euid != (uid_t) -1) {
  1012. new_euid = euid;
  1013. if ((old_ruid != euid) &&
  1014. (current->euid != euid) &&
  1015. (current->suid != euid) &&
  1016. !capable(CAP_SETUID))
  1017. return -EPERM;
  1018. }
  1019. if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
  1020. return -EAGAIN;
  1021. if (new_euid != old_euid) {
  1022. set_dumpable(current->mm, suid_dumpable);
  1023. smp_wmb();
  1024. }
  1025. current->fsuid = current->euid = new_euid;
  1026. if (ruid != (uid_t) -1 ||
  1027. (euid != (uid_t) -1 && euid != old_ruid))
  1028. current->suid = current->euid;
  1029. current->fsuid = current->euid;
  1030. key_fsuid_changed(current);
  1031. proc_id_connector(current, PROC_EVENT_UID);
  1032. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
  1033. }
  1034. /*
  1035. * setuid() is implemented like SysV with SAVED_IDS
  1036. *
  1037. * Note that SAVED_ID's is deficient in that a setuid root program
  1038. * like sendmail, for example, cannot set its uid to be a normal
  1039. * user and then switch back, because if you're root, setuid() sets
  1040. * the saved uid too. If you don't like this, blame the bright people
  1041. * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
  1042. * will allow a root program to temporarily drop privileges and be able to
  1043. * regain them by swapping the real and effective uid.
  1044. */
  1045. asmlinkage long sys_setuid(uid_t uid)
  1046. {
  1047. int old_euid = current->euid;
  1048. int old_ruid, old_suid, new_suid;
  1049. int retval;
  1050. retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
  1051. if (retval)
  1052. return retval;
  1053. old_ruid = current->uid;
  1054. old_suid = current->suid;
  1055. new_suid = old_suid;
  1056. if (capable(CAP_SETUID)) {
  1057. if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
  1058. return -EAGAIN;
  1059. new_suid = uid;
  1060. } else if ((uid != current->uid) && (uid != new_suid))
  1061. return -EPERM;
  1062. if (old_euid != uid) {
  1063. set_dumpable(current->mm, suid_dumpable);
  1064. smp_wmb();
  1065. }
  1066. current->fsuid = current->euid = uid;
  1067. current->suid = new_suid;
  1068. key_fsuid_changed(current);
  1069. proc_id_connector(current, PROC_EVENT_UID);
  1070. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
  1071. }
  1072. /*
  1073. * This function implements a generic ability to update ruid, euid,
  1074. * and suid. This allows you to implement the 4.4 compatible seteuid().
  1075. */
  1076. asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
  1077. {
  1078. int old_ruid = current->uid;
  1079. int old_euid = current->euid;
  1080. int old_suid = current->suid;
  1081. int retval;
  1082. retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
  1083. if (retval)
  1084. return retval;
  1085. if (!capable(CAP_SETUID)) {
  1086. if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
  1087. (ruid != current->euid) && (ruid != current->suid))
  1088. return -EPERM;
  1089. if ((euid != (uid_t) -1) && (euid != current->uid) &&
  1090. (euid != current->euid) && (euid != current->suid))
  1091. return -EPERM;
  1092. if ((suid != (uid_t) -1) && (suid != current->uid) &&
  1093. (suid != current->euid) && (suid != current->suid))
  1094. return -EPERM;
  1095. }
  1096. if (ruid != (uid_t) -1) {
  1097. if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
  1098. return -EAGAIN;
  1099. }
  1100. if (euid != (uid_t) -1) {
  1101. if (euid != current->euid) {
  1102. set_dumpable(current->mm, suid_dumpable);
  1103. smp_wmb();
  1104. }
  1105. current->euid = euid;
  1106. }
  1107. current->fsuid = current->euid;
  1108. if (suid != (uid_t) -1)
  1109. current->suid = suid;
  1110. key_fsuid_changed(current);
  1111. proc_id_connector(current, PROC_EVENT_UID);
  1112. return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
  1113. }
  1114. asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
  1115. {
  1116. int retval;
  1117. if (!(retval = put_user(current->uid, ruid)) &&
  1118. !(retval = put_user(current->euid, euid)))
  1119. retval = put_user(current->suid, suid);
  1120. return retval;
  1121. }
  1122. /*
  1123. * Same as above, but for rgid, egid, sgid.
  1124. */
  1125. asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
  1126. {
  1127. int retval;
  1128. retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
  1129. if (retval)
  1130. return retval;
  1131. if (!capable(CAP_SETGID)) {
  1132. if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
  1133. (rgid != current->egid) && (rgid != current->sgid))
  1134. return -EPERM;
  1135. if ((egid != (gid_t) -1) && (egid != current->gid) &&
  1136. (egid != current->egid) && (egid != current->sgid))
  1137. return -EPERM;
  1138. if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
  1139. (sgid != current->egid) && (sgid != current->sgid))
  1140. return -EPERM;
  1141. }
  1142. if (egid != (gid_t) -1) {
  1143. if (egid != current->egid) {
  1144. set_dumpable(current->mm, suid_dumpable);
  1145. smp_wmb();
  1146. }
  1147. current->egid = egid;
  1148. }
  1149. current->fsgid = current->egid;
  1150. if (rgid != (gid_t) -1)
  1151. current->gid = rgid;
  1152. if (sgid != (gid_t) -1)
  1153. current->sgid = sgid;
  1154. key_fsgid_changed(current);
  1155. proc_id_connector(current, PROC_EVENT_GID);
  1156. return 0;
  1157. }
  1158. asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
  1159. {
  1160. int retval;
  1161. if (!(retval = put_user(current->gid, rgid)) &&
  1162. !(retval = put_user(current->egid, egid)))
  1163. retval = put_user(current->sgid, sgid);
  1164. return retval;
  1165. }
  1166. /*
  1167. * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
  1168. * is used for "access()" and for the NFS daemon (letting nfsd stay at
  1169. * whatever uid it wants to). It normally shadows "euid", except when
  1170. * explicitly set by setfsuid() or for access..
  1171. */
  1172. asmlinkage long sys_setfsuid(uid_t uid)
  1173. {
  1174. int old_fsuid;
  1175. old_fsuid = current->fsuid;
  1176. if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
  1177. return old_fsuid;
  1178. if (uid == current->uid || uid == current->euid ||
  1179. uid == current->suid || uid == current->fsuid ||
  1180. capable(CAP_SETUID)) {
  1181. if (uid != old_fsuid) {
  1182. set_dumpable(current->mm, suid_dumpable);
  1183. smp_wmb();
  1184. }
  1185. current->fsuid = uid;
  1186. }
  1187. key_fsuid_changed(current);
  1188. proc_id_connector(current, PROC_EVENT_UID);
  1189. security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
  1190. return old_fsuid;
  1191. }
  1192. /*
  1193. * Samma på svenska..
  1194. */
  1195. asmlinkage long sys_setfsgid(gid_t gid)
  1196. {
  1197. int old_fsgid;
  1198. old_fsgid = current->fsgid;
  1199. if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
  1200. return old_fsgid;
  1201. if (gid == current->gid || gid == current->egid ||
  1202. gid == current->sgid || gid == current->fsgid ||
  1203. capable(CAP_SETGID)) {
  1204. if (gid != old_fsgid) {
  1205. set_dumpable(current->mm, suid_dumpable);
  1206. smp_wmb();
  1207. }
  1208. current->fsgid = gid;
  1209. key_fsgid_changed(current);
  1210. proc_id_connector(current, PROC_EVENT_GID);
  1211. }
  1212. return old_fsgid;
  1213. }
  1214. asmlinkage long sys_times(struct tms __user * tbuf)
  1215. {
  1216. /*
  1217. * In the SMP world we might just be unlucky and have one of
  1218. * the times increment as we use it. Since the value is an
  1219. * atomically safe type this is just fine. Conceptually its
  1220. * as if the syscall took an instant longer to occur.
  1221. */
  1222. if (tbuf) {
  1223. struct tms tmp;
  1224. struct task_struct *tsk = current;
  1225. struct task_struct *t;
  1226. cputime_t utime, stime, cutime, cstime;
  1227. spin_lock_irq(&tsk->sighand->siglock);
  1228. utime = tsk->signal->utime;
  1229. stime = tsk->signal->stime;
  1230. t = tsk;
  1231. do {
  1232. utime = cputime_add(utime, t->utime);
  1233. stime = cputime_add(stime, t->stime);
  1234. t = next_thread(t);
  1235. } while (t != tsk);
  1236. cutime = tsk->signal->cutime;
  1237. cstime = tsk->signal->cstime;
  1238. spin_unlock_irq(&tsk->sighand->siglock);
  1239. tmp.tms_utime = cputime_to_clock_t(utime);
  1240. tmp.tms_stime = cputime_to_clock_t(stime);
  1241. tmp.tms_cutime = cputime_to_clock_t(cutime);
  1242. tmp.tms_cstime = cputime_to_clock_t(cstime);
  1243. if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
  1244. return -EFAULT;
  1245. }
  1246. return (long) jiffies_64_to_clock_t(get_jiffies_64());
  1247. }
  1248. /*
  1249. * This needs some heavy checking ...
  1250. * I just haven't the stomach for it. I also don't fully
  1251. * understand sessions/pgrp etc. Let somebody who does explain it.
  1252. *
  1253. * OK, I think I have the protection semantics right.... this is really
  1254. * only important on a multi-user system anyway, to make sure one user
  1255. * can't send a signal to a process owned by another. -TYT, 12/12/91
  1256. *
  1257. * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
  1258. * LBT 04.03.94
  1259. */
  1260. asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
  1261. {
  1262. struct task_struct *p;
  1263. struct task_struct *group_leader = current->group_leader;
  1264. int err = -EINVAL;
  1265. if (!pid)
  1266. pid = group_leader->pid;
  1267. if (!pgid)
  1268. pgid = pid;
  1269. if (pgid < 0)
  1270. return -EINVAL;
  1271. /* From this point forward we keep holding onto the tasklist lock
  1272. * so that our parent does not change from under us. -DaveM
  1273. */
  1274. write_lock_irq(&tasklist_lock);
  1275. err = -ESRCH;
  1276. p = find_task_by_pid(pid);
  1277. if (!p)
  1278. goto out;
  1279. err = -EINVAL;
  1280. if (!thread_group_leader(p))
  1281. goto out;
  1282. if (p->real_parent == group_leader) {
  1283. err = -EPERM;
  1284. if (task_session(p) != task_session(group_leader))
  1285. goto out;
  1286. err = -EACCES;
  1287. if (p->did_exec)
  1288. goto out;
  1289. } else {
  1290. err = -ESRCH;
  1291. if (p != group_leader)
  1292. goto out;
  1293. }
  1294. err = -EPERM;
  1295. if (p->signal->leader)
  1296. goto out;
  1297. if (pgid != pid) {
  1298. struct task_struct *g =
  1299. find_task_by_pid_type(PIDTYPE_PGID, pgid);
  1300. if (!g || task_session(g) != task_session(group_leader))
  1301. goto out;
  1302. }
  1303. err = security_task_setpgid(p, pgid);
  1304. if (err)
  1305. goto out;
  1306. if (process_group(p) != pgid) {
  1307. detach_pid(p, PIDTYPE_PGID);
  1308. p->signal->pgrp = pgid;
  1309. attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
  1310. }
  1311. err = 0;
  1312. out:
  1313. /* All paths lead to here, thus we are safe. -DaveM */
  1314. write_unlock_irq(&tasklist_lock);
  1315. return err;
  1316. }
  1317. asmlinkage long sys_getpgid(pid_t pid)
  1318. {
  1319. if (!pid)
  1320. return process_group(current);
  1321. else {
  1322. int retval;
  1323. struct task_struct *p;
  1324. read_lock(&tasklist_lock);
  1325. p = find_task_by_pid(pid);
  1326. retval = -ESRCH;
  1327. if (p) {
  1328. retval = security_task_getpgid(p);
  1329. if (!retval)
  1330. retval = process_group(p);
  1331. }
  1332. read_unlock(&tasklist_lock);
  1333. return retval;
  1334. }
  1335. }
  1336. #ifdef __ARCH_WANT_SYS_GETPGRP
  1337. asmlinkage long sys_getpgrp(void)
  1338. {
  1339. /* SMP - assuming writes are word atomic this is fine */
  1340. return process_group(current);
  1341. }
  1342. #endif
  1343. asmlinkage long sys_getsid(pid_t pid)
  1344. {
  1345. if (!pid)
  1346. return process_session(current);
  1347. else {
  1348. int retval;
  1349. struct task_struct *p;
  1350. read_lock(&tasklist_lock);
  1351. p = find_task_by_pid(pid);
  1352. retval = -ESRCH;
  1353. if (p) {
  1354. retval = security_task_getsid(p);
  1355. if (!retval)
  1356. retval = process_session(p);
  1357. }
  1358. read_unlock(&tasklist_lock);
  1359. return retval;
  1360. }
  1361. }
  1362. asmlinkage long sys_setsid(void)
  1363. {
  1364. struct task_struct *group_leader = current->group_leader;
  1365. pid_t session;
  1366. int err = -EPERM;
  1367. write_lock_irq(&tasklist_lock);
  1368. /* Fail if I am already a session leader */
  1369. if (group_leader->signal->leader)
  1370. goto out;
  1371. session = group_leader->pid;
  1372. /* Fail if a process group id already exists that equals the
  1373. * proposed session id.
  1374. *
  1375. * Don't check if session id == 1 because kernel threads use this
  1376. * session id and so the check will always fail and make it so
  1377. * init cannot successfully call setsid.
  1378. */
  1379. if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
  1380. goto out;
  1381. group_leader->signal->leader = 1;
  1382. __set_special_pids(session, session);
  1383. spin_lock(&group_leader->sighand->siglock);
  1384. group_leader->signal->tty = NULL;
  1385. spin_unlock(&group_leader->sighand->siglock);
  1386. err = process_group(group_leader);
  1387. out:
  1388. write_unlock_irq(&tasklist_lock);
  1389. return err;
  1390. }
  1391. /*
  1392. * Supplementary group IDs
  1393. */
  1394. /* init to 2 - one for init_task, one to ensure it is never freed */
  1395. struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
  1396. struct group_info *groups_alloc(int gidsetsize)
  1397. {
  1398. struct group_info *group_info;
  1399. int nblocks;
  1400. int i;
  1401. nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
  1402. /* Make sure we always allocate at least one indirect block pointer */
  1403. nblocks = nblocks ? : 1;
  1404. group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
  1405. if (!group_info)
  1406. return NULL;
  1407. group_info->ngroups = gidsetsize;
  1408. group_info->nblocks = nblocks;
  1409. atomic_set(&group_info->usage, 1);
  1410. if (gidsetsize <= NGROUPS_SMALL)
  1411. group_info->blocks[0] = group_info->small_block;
  1412. else {
  1413. for (i = 0; i < nblocks; i++) {
  1414. gid_t *b;
  1415. b = (void *)__get_free_page(GFP_USER);
  1416. if (!b)
  1417. goto out_undo_partial_alloc;
  1418. group_info->blocks[i] = b;
  1419. }
  1420. }
  1421. return group_info;
  1422. out_undo_partial_alloc:
  1423. while (--i >= 0) {
  1424. free_page((unsigned long)group_info->blocks[i]);
  1425. }
  1426. kfree(group_info);
  1427. return NULL;
  1428. }
  1429. EXPORT_SYMBOL(groups_alloc);
  1430. void groups_free(struct group_info *group_info)
  1431. {
  1432. if (group_info->blocks[0] != group_info->small_block) {
  1433. int i;
  1434. for (i = 0; i < group_info->nblocks; i++)
  1435. free_page((unsigned long)group_info->blocks[i]);
  1436. }
  1437. kfree(group_info);
  1438. }
  1439. EXPORT_SYMBOL(groups_free);
  1440. /* export the group_info to a user-space array */
  1441. static int groups_to_user(gid_t __user *grouplist,
  1442. struct group_info *group_info)
  1443. {
  1444. int i;
  1445. int count = group_info->ngroups;
  1446. for (i = 0; i < group_info->nblocks; i++) {
  1447. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1448. int off = i * NGROUPS_PER_BLOCK;
  1449. int len = cp_count * sizeof(*grouplist);
  1450. if (copy_to_user(grouplist+off, group_info->blocks[i], len))
  1451. return -EFAULT;
  1452. count -= cp_count;
  1453. }
  1454. return 0;
  1455. }
  1456. /* fill a group_info from a user-space array - it must be allocated already */
  1457. static int groups_from_user(struct group_info *group_info,
  1458. gid_t __user *grouplist)
  1459. {
  1460. int i;
  1461. int count = group_info->ngroups;
  1462. for (i = 0; i < group_info->nblocks; i++) {
  1463. int cp_count = min(NGROUPS_PER_BLOCK, count);
  1464. int off = i * NGROUPS_PER_BLOCK;
  1465. int len = cp_count * sizeof(*grouplist);
  1466. if (copy_from_user(group_info->blocks[i], grouplist+off, len))
  1467. return -EFAULT;
  1468. count -= cp_count;
  1469. }
  1470. return 0;
  1471. }
  1472. /* a simple Shell sort */
  1473. static void groups_sort(struct group_info *group_info)
  1474. {
  1475. int base, max, stride;
  1476. int gidsetsize = group_info->ngroups;
  1477. for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
  1478. ; /* nothing */
  1479. stride /= 3;
  1480. while (stride) {
  1481. max = gidsetsize - stride;
  1482. for (base = 0; base < max; base++) {
  1483. int left = base;
  1484. int right = left + stride;
  1485. gid_t tmp = GROUP_AT(group_info, right);
  1486. while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
  1487. GROUP_AT(group_info, right) =
  1488. GROUP_AT(group_info, left);
  1489. right = left;
  1490. left -= stride;
  1491. }
  1492. GROUP_AT(group_info, right) = tmp;
  1493. }
  1494. stride /= 3;
  1495. }
  1496. }
  1497. /* a simple bsearch */
  1498. int groups_search(struct group_info *group_info, gid_t grp)
  1499. {
  1500. unsigned int left, right;
  1501. if (!group_info)
  1502. return 0;
  1503. left = 0;
  1504. right = group_info->ngroups;
  1505. while (left < right) {
  1506. unsigned int mid = (left+right)/2;
  1507. int cmp = grp - GROUP_AT(group_info, mid);
  1508. if (cmp > 0)
  1509. left = mid + 1;
  1510. else if (cmp < 0)
  1511. right = mid;
  1512. else
  1513. return 1;
  1514. }
  1515. return 0;
  1516. }
  1517. /* validate and set current->group_info */
  1518. int set_current_groups(struct group_info *group_info)
  1519. {
  1520. int retval;
  1521. struct group_info *old_info;
  1522. retval = security_task_setgroups(group_info);
  1523. if (retval)
  1524. return retval;
  1525. groups_sort(group_info);
  1526. get_group_info(group_info);
  1527. task_lock(current);
  1528. old_info = current->group_info;
  1529. current->group_info = group_info;
  1530. task_unlock(current);
  1531. put_group_info(old_info);
  1532. return 0;
  1533. }
  1534. EXPORT_SYMBOL(set_current_groups);
  1535. asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
  1536. {
  1537. int i = 0;
  1538. /*
  1539. * SMP: Nobody else can change our grouplist. Thus we are
  1540. * safe.
  1541. */
  1542. if (gidsetsize < 0)
  1543. return -EINVAL;
  1544. /* no need to grab task_lock here; it cannot change */
  1545. i = current->group_info->ngroups;
  1546. if (gidsetsize) {
  1547. if (i > gidsetsize) {
  1548. i = -EINVAL;
  1549. goto out;
  1550. }
  1551. if (groups_to_user(grouplist, current->group_info)) {
  1552. i = -EFAULT;
  1553. goto out;
  1554. }
  1555. }
  1556. out:
  1557. return i;
  1558. }
  1559. /*
  1560. * SMP: Our groups are copy-on-write. We can set them safely
  1561. * without another task interfering.
  1562. */
  1563. asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
  1564. {
  1565. struct group_info *group_info;
  1566. int retval;
  1567. if (!capable(CAP_SETGID))
  1568. return -EPERM;
  1569. if ((unsigned)gidsetsize > NGROUPS_MAX)
  1570. return -EINVAL;
  1571. group_info = groups_alloc(gidsetsize);
  1572. if (!group_info)
  1573. return -ENOMEM;
  1574. retval = groups_from_user(group_info, grouplist);
  1575. if (retval) {
  1576. put_group_info(group_info);
  1577. return retval;
  1578. }
  1579. retval = set_current_groups(group_info);
  1580. put_group_info(group_info);
  1581. return retval;
  1582. }
  1583. /*
  1584. * Check whether we're fsgid/egid or in the supplemental group..
  1585. */
  1586. int in_group_p(gid_t grp)
  1587. {
  1588. int retval = 1;
  1589. if (grp != current->fsgid)
  1590. retval = groups_search(current->group_info, grp);
  1591. return retval;
  1592. }
  1593. EXPORT_SYMBOL(in_group_p);
  1594. int in_egroup_p(gid_t grp)
  1595. {
  1596. int retval = 1;
  1597. if (grp != current->egid)
  1598. retval = groups_search(current->group_info, grp);
  1599. return retval;
  1600. }
  1601. EXPORT_SYMBOL(in_egroup_p);
  1602. DECLARE_RWSEM(uts_sem);
  1603. EXPORT_SYMBOL(uts_sem);
  1604. asmlinkage long sys_newuname(struct new_utsname __user * name)
  1605. {
  1606. int errno = 0;
  1607. down_read(&uts_sem);
  1608. if (copy_to_user(name, utsname(), sizeof *name))
  1609. errno = -EFAULT;
  1610. up_read(&uts_sem);
  1611. return errno;
  1612. }
  1613. asmlinkage long sys_sethostname(char __user *name, int len)
  1614. {
  1615. int errno;
  1616. char tmp[__NEW_UTS_LEN];
  1617. if (!capable(CAP_SYS_ADMIN))
  1618. return -EPERM;
  1619. if (len < 0 || len > __NEW_UTS_LEN)
  1620. return -EINVAL;
  1621. down_write(&uts_sem);
  1622. errno = -EFAULT;
  1623. if (!copy_from_user(tmp, name, len)) {
  1624. memcpy(utsname()->nodename, tmp, len);
  1625. utsname()->nodename[len] = 0;
  1626. errno = 0;
  1627. }
  1628. up_write(&uts_sem);
  1629. return errno;
  1630. }
  1631. #ifdef __ARCH_WANT_SYS_GETHOSTNAME
  1632. asmlinkage long sys_gethostname(char __user *name, int len)
  1633. {
  1634. int i, errno;
  1635. if (len < 0)
  1636. return -EINVAL;
  1637. down_read(&uts_sem);
  1638. i = 1 + strlen(utsname()->nodename);
  1639. if (i > len)
  1640. i = len;
  1641. errno = 0;
  1642. if (copy_to_user(name, utsname()->nodename, i))
  1643. errno = -EFAULT;
  1644. up_read(&uts_sem);
  1645. return errno;
  1646. }
  1647. #endif
  1648. /*
  1649. * Only setdomainname; getdomainname can be implemented by calling
  1650. * uname()
  1651. */
  1652. asmlinkage long sys_setdomainname(char __user *name, int len)
  1653. {
  1654. int errno;
  1655. char tmp[__NEW_UTS_LEN];
  1656. if (!capable(CAP_SYS_ADMIN))
  1657. return -EPERM;
  1658. if (len < 0 || len > __NEW_UTS_LEN)
  1659. return -EINVAL;
  1660. down_write(&uts_sem);
  1661. errno = -EFAULT;
  1662. if (!copy_from_user(tmp, name, len)) {
  1663. memcpy(utsname()->domainname, tmp, len);
  1664. utsname()->domainname[len] = 0;
  1665. errno = 0;
  1666. }
  1667. up_write(&uts_sem);
  1668. return errno;
  1669. }
  1670. asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1671. {
  1672. if (resource >= RLIM_NLIMITS)
  1673. return -EINVAL;
  1674. else {
  1675. struct rlimit value;
  1676. task_lock(current->group_leader);
  1677. value = current->signal->rlim[resource];
  1678. task_unlock(current->group_leader);
  1679. return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
  1680. }
  1681. }
  1682. #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
  1683. /*
  1684. * Back compatibility for getrlimit. Needed for some apps.
  1685. */
  1686. asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
  1687. {
  1688. struct rlimit x;
  1689. if (resource >= RLIM_NLIMITS)
  1690. return -EINVAL;
  1691. task_lock(current->group_leader);
  1692. x = current->signal->rlim[resource];
  1693. task_unlock(current->group_leader);
  1694. if (x.rlim_cur > 0x7FFFFFFF)
  1695. x.rlim_cur = 0x7FFFFFFF;
  1696. if (x.rlim_max > 0x7FFFFFFF)
  1697. x.rlim_max = 0x7FFFFFFF;
  1698. return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
  1699. }
  1700. #endif
  1701. asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
  1702. {
  1703. struct rlimit new_rlim, *old_rlim;
  1704. unsigned long it_prof_secs;
  1705. int retval;
  1706. if (resource >= RLIM_NLIMITS)
  1707. return -EINVAL;
  1708. if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
  1709. return -EFAULT;
  1710. if (new_rlim.rlim_cur > new_rlim.rlim_max)
  1711. return -EINVAL;
  1712. old_rlim = current->signal->rlim + resource;
  1713. if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
  1714. !capable(CAP_SYS_RESOURCE))
  1715. return -EPERM;
  1716. if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
  1717. return -EPERM;
  1718. retval = security_task_setrlimit(resource, &new_rlim);
  1719. if (retval)
  1720. return retval;
  1721. if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
  1722. /*
  1723. * The caller is asking for an immediate RLIMIT_CPU
  1724. * expiry. But we use the zero value to mean "it was
  1725. * never set". So let's cheat and make it one second
  1726. * instead
  1727. */
  1728. new_rlim.rlim_cur = 1;
  1729. }
  1730. task_lock(current->group_leader);
  1731. *old_rlim = new_rlim;
  1732. task_unlock(current->group_leader);
  1733. if (resource != RLIMIT_CPU)
  1734. goto out;
  1735. /*
  1736. * RLIMIT_CPU handling. Note that the kernel fails to return an error
  1737. * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
  1738. * very long-standing error, and fixing it now risks breakage of
  1739. * applications, so we live with it
  1740. */
  1741. if (new_rlim.rlim_cur == RLIM_INFINITY)
  1742. goto out;
  1743. it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
  1744. if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
  1745. unsigned long rlim_cur = new_rlim.rlim_cur;
  1746. cputime_t cputime;
  1747. cputime = secs_to_cputime(rlim_cur);
  1748. read_lock(&tasklist_lock);
  1749. spin_lock_irq(&current->sighand->siglock);
  1750. set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
  1751. spin_unlock_irq(&current->sighand->siglock);
  1752. read_unlock(&tasklist_lock);
  1753. }
  1754. out:
  1755. return 0;
  1756. }
  1757. /*
  1758. * It would make sense to put struct rusage in the task_struct,
  1759. * except that would make the task_struct be *really big*. After
  1760. * task_struct gets moved into malloc'ed memory, it would
  1761. * make sense to do this. It will make moving the rest of the information
  1762. * a lot simpler! (Which we're not doing right now because we're not
  1763. * measuring them yet).
  1764. *
  1765. * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
  1766. * races with threads incrementing their own counters. But since word
  1767. * reads are atomic, we either get new values or old values and we don't
  1768. * care which for the sums. We always take the siglock to protect reading
  1769. * the c* fields from p->signal from races with exit.c updating those
  1770. * fields when reaping, so a sample either gets all the additions of a
  1771. * given child after it's reaped, or none so this sample is before reaping.
  1772. *
  1773. * Locking:
  1774. * We need to take the siglock for CHILDEREN, SELF and BOTH
  1775. * for the cases current multithreaded, non-current single threaded
  1776. * non-current multithreaded. Thread traversal is now safe with
  1777. * the siglock held.
  1778. * Strictly speaking, we donot need to take the siglock if we are current and
  1779. * single threaded, as no one else can take our signal_struct away, no one
  1780. * else can reap the children to update signal->c* counters, and no one else
  1781. * can race with the signal-> fields. If we do not take any lock, the
  1782. * signal-> fields could be read out of order while another thread was just
  1783. * exiting. So we should place a read memory barrier when we avoid the lock.
  1784. * On the writer side, write memory barrier is implied in __exit_signal
  1785. * as __exit_signal releases the siglock spinlock after updating the signal->
  1786. * fields. But we don't do this yet to keep things simple.
  1787. *
  1788. */
  1789. static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
  1790. {
  1791. struct task_struct *t;
  1792. unsigned long flags;
  1793. cputime_t utime, stime;
  1794. memset((char *) r, 0, sizeof *r);
  1795. utime = stime = cputime_zero;
  1796. rcu_read_lock();
  1797. if (!lock_task_sighand(p, &flags)) {
  1798. rcu_read_unlock();
  1799. return;
  1800. }
  1801. switch (who) {
  1802. case RUSAGE_BOTH:
  1803. case RUSAGE_CHILDREN:
  1804. utime = p->signal->cutime;
  1805. stime = p->signal->cstime;
  1806. r->ru_nvcsw = p->signal->cnvcsw;
  1807. r->ru_nivcsw = p->signal->cnivcsw;
  1808. r->ru_minflt = p->signal->cmin_flt;
  1809. r->ru_majflt = p->signal->cmaj_flt;
  1810. r->ru_inblock = p->signal->cinblock;
  1811. r->ru_oublock = p->signal->coublock;
  1812. if (who == RUSAGE_CHILDREN)
  1813. break;
  1814. case RUSAGE_SELF:
  1815. utime = cputime_add(utime, p->signal->utime);
  1816. stime = cputime_add(stime, p->signal->stime);
  1817. r->ru_nvcsw += p->signal->nvcsw;
  1818. r->ru_nivcsw += p->signal->nivcsw;
  1819. r->ru_minflt += p->signal->min_flt;
  1820. r->ru_majflt += p->signal->maj_flt;
  1821. r->ru_inblock += p->signal->inblock;
  1822. r->ru_oublock += p->signal->oublock;
  1823. t = p;
  1824. do {
  1825. utime = cputime_add(utime, t->utime);
  1826. stime = cputime_add(stime, t->stime);
  1827. r->ru_nvcsw += t->nvcsw;
  1828. r->ru_nivcsw += t->nivcsw;
  1829. r->ru_minflt += t->min_flt;
  1830. r->ru_majflt += t->maj_flt;
  1831. r->ru_inblock += task_io_get_inblock(t);
  1832. r->ru_oublock += task_io_get_oublock(t);
  1833. t = next_thread(t);
  1834. } while (t != p);
  1835. break;
  1836. default:
  1837. BUG();
  1838. }
  1839. unlock_task_sighand(p, &flags);
  1840. rcu_read_unlock();
  1841. cputime_to_timeval(utime, &r->ru_utime);
  1842. cputime_to_timeval(stime, &r->ru_stime);
  1843. }
  1844. int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
  1845. {
  1846. struct rusage r;
  1847. k_getrusage(p, who, &r);
  1848. return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
  1849. }
  1850. asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
  1851. {
  1852. if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
  1853. return -EINVAL;
  1854. return getrusage(current, who, ru);
  1855. }
  1856. asmlinkage long sys_umask(int mask)
  1857. {
  1858. mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
  1859. return mask;
  1860. }
  1861. asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
  1862. unsigned long arg4, unsigned long arg5)
  1863. {
  1864. long error;
  1865. error = security_task_prctl(option, arg2, arg3, arg4, arg5);
  1866. if (error)
  1867. return error;
  1868. switch (option) {
  1869. case PR_SET_PDEATHSIG:
  1870. if (!valid_signal(arg2)) {
  1871. error = -EINVAL;
  1872. break;
  1873. }
  1874. current->pdeath_signal = arg2;
  1875. break;
  1876. case PR_GET_PDEATHSIG:
  1877. error = put_user(current->pdeath_signal, (int __user *)arg2);
  1878. break;
  1879. case PR_GET_DUMPABLE:
  1880. error = get_dumpable(current->mm);
  1881. break;
  1882. case PR_SET_DUMPABLE:
  1883. if (arg2 < 0 || arg2 > 1) {
  1884. error = -EINVAL;
  1885. break;
  1886. }
  1887. set_dumpable(current->mm, arg2);
  1888. break;
  1889. case PR_SET_UNALIGN:
  1890. error = SET_UNALIGN_CTL(current, arg2);
  1891. break;
  1892. case PR_GET_UNALIGN:
  1893. error = GET_UNALIGN_CTL(current, arg2);
  1894. break;
  1895. case PR_SET_FPEMU:
  1896. error = SET_FPEMU_CTL(current, arg2);
  1897. break;
  1898. case PR_GET_FPEMU:
  1899. error = GET_FPEMU_CTL(current, arg2);
  1900. break;
  1901. case PR_SET_FPEXC:
  1902. error = SET_FPEXC_CTL(current, arg2);
  1903. break;
  1904. case PR_GET_FPEXC:
  1905. error = GET_FPEXC_CTL(current, arg2);
  1906. break;
  1907. case PR_GET_TIMING:
  1908. error = PR_TIMING_STATISTICAL;
  1909. break;
  1910. case PR_SET_TIMING:
  1911. if (arg2 == PR_TIMING_STATISTICAL)
  1912. error = 0;
  1913. else
  1914. error = -EINVAL;
  1915. break;
  1916. case PR_GET_KEEPCAPS:
  1917. if (current->keep_capabilities)
  1918. error = 1;
  1919. break;
  1920. case PR_SET_KEEPCAPS:
  1921. if (arg2 != 0 && arg2 != 1) {
  1922. error = -EINVAL;
  1923. break;
  1924. }
  1925. current->keep_capabilities = arg2;
  1926. break;
  1927. case PR_SET_NAME: {
  1928. struct task_struct *me = current;
  1929. unsigned char ncomm[sizeof(me->comm)];
  1930. ncomm[sizeof(me->comm)-1] = 0;
  1931. if (strncpy_from_user(ncomm, (char __user *)arg2,
  1932. sizeof(me->comm)-1) < 0)
  1933. return -EFAULT;
  1934. set_task_comm(me, ncomm);
  1935. return 0;
  1936. }
  1937. case PR_GET_NAME: {
  1938. struct task_struct *me = current;
  1939. unsigned char tcomm[sizeof(me->comm)];
  1940. get_task_comm(tcomm, me);
  1941. if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
  1942. return -EFAULT;
  1943. return 0;
  1944. }
  1945. case PR_GET_ENDIAN:
  1946. error = GET_ENDIAN(current, arg2);
  1947. break;
  1948. case PR_SET_ENDIAN:
  1949. error = SET_ENDIAN(current, arg2);
  1950. break;
  1951. case PR_GET_SECCOMP:
  1952. error = prctl_get_seccomp();
  1953. break;
  1954. case PR_SET_SECCOMP:
  1955. error = prctl_set_seccomp(arg2);
  1956. break;
  1957. default:
  1958. error = -EINVAL;
  1959. break;
  1960. }
  1961. return error;
  1962. }
  1963. asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
  1964. struct getcpu_cache __user *cache)
  1965. {
  1966. int err = 0;
  1967. int cpu = raw_smp_processor_id();
  1968. if (cpup)
  1969. err |= put_user(cpu, cpup);
  1970. if (nodep)
  1971. err |= put_user(cpu_to_node(cpu), nodep);
  1972. if (cache) {
  1973. /*
  1974. * The cache is not needed for this implementation,
  1975. * but make sure user programs pass something
  1976. * valid. vsyscall implementations can instead make
  1977. * good use of the cache. Only use t0 and t1 because
  1978. * these are available in both 32bit and 64bit ABI (no
  1979. * need for a compat_getcpu). 32bit has enough
  1980. * padding
  1981. */
  1982. unsigned long t0, t1;
  1983. get_user(t0, &cache->blob[0]);
  1984. get_user(t1, &cache->blob[1]);
  1985. t0++;
  1986. t1++;
  1987. put_user(t0, &cache->blob[0]);
  1988. put_user(t1, &cache->blob[1]);
  1989. }
  1990. return err ? -EFAULT : 0;
  1991. }
  1992. char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
  1993. static void argv_cleanup(char **argv, char **envp)
  1994. {
  1995. argv_free(argv);
  1996. }
  1997. /**
  1998. * orderly_poweroff - Trigger an orderly system poweroff
  1999. * @force: force poweroff if command execution fails
  2000. *
  2001. * This may be called from any context to trigger a system shutdown.
  2002. * If the orderly shutdown fails, it will force an immediate shutdown.
  2003. */
  2004. int orderly_poweroff(bool force)
  2005. {
  2006. int argc;
  2007. char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
  2008. static char *envp[] = {
  2009. "HOME=/",
  2010. "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
  2011. NULL
  2012. };
  2013. int ret = -ENOMEM;
  2014. struct subprocess_info *info;
  2015. if (argv == NULL) {
  2016. printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
  2017. __func__, poweroff_cmd);
  2018. goto out;
  2019. }
  2020. info = call_usermodehelper_setup(argv[0], argv, envp);
  2021. if (info == NULL) {
  2022. argv_free(argv);
  2023. goto out;
  2024. }
  2025. call_usermodehelper_setcleanup(info, argv_cleanup);
  2026. ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
  2027. out:
  2028. if (ret && force) {
  2029. printk(KERN_WARNING "Failed to start orderly shutdown: "
  2030. "forcing the issue\n");
  2031. /* I guess this should try to kick off some daemon to
  2032. sync and poweroff asap. Or not even bother syncing
  2033. if we're doing an emergency shutdown? */
  2034. emergency_sync();
  2035. kernel_power_off();
  2036. }
  2037. return ret;
  2038. }
  2039. EXPORT_SYMBOL_GPL(orderly_poweroff);