cred.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* Task credentials management - see Documentation/credentials.txt
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/cred.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include <linux/key.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/init_task.h>
  18. #include <linux/security.h>
  19. #include <linux/cn_proc.h>
  20. #include "cred-internals.h"
  21. #if 0
  22. #define kdebug(FMT, ...) \
  23. printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  24. #else
  25. static inline __attribute__((format(printf, 1, 2)))
  26. void no_printk(const char *fmt, ...)
  27. {
  28. }
  29. #define kdebug(FMT, ...) \
  30. no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  31. #endif
  32. static struct kmem_cache *cred_jar;
  33. /*
  34. * The common credentials for the initial task's thread group
  35. */
  36. #ifdef CONFIG_KEYS
  37. static struct thread_group_cred init_tgcred = {
  38. .usage = ATOMIC_INIT(2),
  39. .tgid = 0,
  40. .lock = SPIN_LOCK_UNLOCKED,
  41. };
  42. #endif
  43. /*
  44. * The initial credentials for the initial task
  45. */
  46. struct cred init_cred = {
  47. .usage = ATOMIC_INIT(4),
  48. #ifdef CONFIG_DEBUG_CREDENTIALS
  49. .subscribers = ATOMIC_INIT(2),
  50. .magic = CRED_MAGIC,
  51. #endif
  52. .securebits = SECUREBITS_DEFAULT,
  53. .cap_inheritable = CAP_INIT_INH_SET,
  54. .cap_permitted = CAP_FULL_SET,
  55. .cap_effective = CAP_INIT_EFF_SET,
  56. .cap_bset = CAP_INIT_BSET,
  57. .user = INIT_USER,
  58. .group_info = &init_groups,
  59. #ifdef CONFIG_KEYS
  60. .tgcred = &init_tgcred,
  61. #endif
  62. };
  63. static inline void set_cred_subscribers(struct cred *cred, int n)
  64. {
  65. #ifdef CONFIG_DEBUG_CREDENTIALS
  66. atomic_set(&cred->subscribers, n);
  67. #endif
  68. }
  69. static inline int read_cred_subscribers(const struct cred *cred)
  70. {
  71. #ifdef CONFIG_DEBUG_CREDENTIALS
  72. return atomic_read(&cred->subscribers);
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. static inline void alter_cred_subscribers(const struct cred *_cred, int n)
  78. {
  79. #ifdef CONFIG_DEBUG_CREDENTIALS
  80. struct cred *cred = (struct cred *) _cred;
  81. atomic_add(n, &cred->subscribers);
  82. #endif
  83. }
  84. /*
  85. * Dispose of the shared task group credentials
  86. */
  87. #ifdef CONFIG_KEYS
  88. static void release_tgcred_rcu(struct rcu_head *rcu)
  89. {
  90. struct thread_group_cred *tgcred =
  91. container_of(rcu, struct thread_group_cred, rcu);
  92. BUG_ON(atomic_read(&tgcred->usage) != 0);
  93. key_put(tgcred->session_keyring);
  94. key_put(tgcred->process_keyring);
  95. kfree(tgcred);
  96. }
  97. #endif
  98. /*
  99. * Release a set of thread group credentials.
  100. */
  101. static void release_tgcred(struct cred *cred)
  102. {
  103. #ifdef CONFIG_KEYS
  104. struct thread_group_cred *tgcred = cred->tgcred;
  105. if (atomic_dec_and_test(&tgcred->usage))
  106. call_rcu(&tgcred->rcu, release_tgcred_rcu);
  107. #endif
  108. }
  109. /*
  110. * The RCU callback to actually dispose of a set of credentials
  111. */
  112. static void put_cred_rcu(struct rcu_head *rcu)
  113. {
  114. struct cred *cred = container_of(rcu, struct cred, rcu);
  115. kdebug("put_cred_rcu(%p)", cred);
  116. #ifdef CONFIG_DEBUG_CREDENTIALS
  117. if (cred->magic != CRED_MAGIC_DEAD ||
  118. atomic_read(&cred->usage) != 0 ||
  119. read_cred_subscribers(cred) != 0)
  120. panic("CRED: put_cred_rcu() sees %p with"
  121. " mag %x, put %p, usage %d, subscr %d\n",
  122. cred, cred->magic, cred->put_addr,
  123. atomic_read(&cred->usage),
  124. read_cred_subscribers(cred));
  125. #else
  126. if (atomic_read(&cred->usage) != 0)
  127. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  128. cred, atomic_read(&cred->usage));
  129. #endif
  130. security_cred_free(cred);
  131. key_put(cred->thread_keyring);
  132. key_put(cred->request_key_auth);
  133. release_tgcred(cred);
  134. if (cred->group_info)
  135. put_group_info(cred->group_info);
  136. free_uid(cred->user);
  137. kmem_cache_free(cred_jar, cred);
  138. }
  139. /**
  140. * __put_cred - Destroy a set of credentials
  141. * @cred: The record to release
  142. *
  143. * Destroy a set of credentials on which no references remain.
  144. */
  145. void __put_cred(struct cred *cred)
  146. {
  147. kdebug("__put_cred(%p{%d,%d})", cred,
  148. atomic_read(&cred->usage),
  149. read_cred_subscribers(cred));
  150. BUG_ON(atomic_read(&cred->usage) != 0);
  151. #ifdef CONFIG_DEBUG_CREDENTIALS
  152. BUG_ON(read_cred_subscribers(cred) != 0);
  153. cred->magic = CRED_MAGIC_DEAD;
  154. cred->put_addr = __builtin_return_address(0);
  155. #endif
  156. BUG_ON(cred == current->cred);
  157. BUG_ON(cred == current->real_cred);
  158. call_rcu(&cred->rcu, put_cred_rcu);
  159. }
  160. EXPORT_SYMBOL(__put_cred);
  161. /*
  162. * Clean up a task's credentials when it exits
  163. */
  164. void exit_creds(struct task_struct *tsk)
  165. {
  166. struct cred *cred;
  167. kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
  168. atomic_read(&tsk->cred->usage),
  169. read_cred_subscribers(tsk->cred));
  170. cred = (struct cred *) tsk->real_cred;
  171. tsk->real_cred = NULL;
  172. validate_creds(cred);
  173. alter_cred_subscribers(cred, -1);
  174. put_cred(cred);
  175. cred = (struct cred *) tsk->cred;
  176. tsk->cred = NULL;
  177. validate_creds(cred);
  178. alter_cred_subscribers(cred, -1);
  179. put_cred(cred);
  180. cred = (struct cred *) tsk->replacement_session_keyring;
  181. if (cred) {
  182. tsk->replacement_session_keyring = NULL;
  183. validate_creds(cred);
  184. put_cred(cred);
  185. }
  186. }
  187. /*
  188. * Allocate blank credentials, such that the credentials can be filled in at a
  189. * later date without risk of ENOMEM.
  190. */
  191. struct cred *cred_alloc_blank(void)
  192. {
  193. struct cred *new;
  194. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  195. if (!new)
  196. return NULL;
  197. #ifdef CONFIG_KEYS
  198. new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);
  199. if (!new->tgcred) {
  200. kmem_cache_free(cred_jar, new);
  201. return NULL;
  202. }
  203. atomic_set(&new->tgcred->usage, 1);
  204. #endif
  205. atomic_set(&new->usage, 1);
  206. if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
  207. goto error;
  208. #ifdef CONFIG_DEBUG_CREDENTIALS
  209. new->magic = CRED_MAGIC;
  210. #endif
  211. return new;
  212. error:
  213. abort_creds(new);
  214. return NULL;
  215. }
  216. /**
  217. * prepare_creds - Prepare a new set of credentials for modification
  218. *
  219. * Prepare a new set of task credentials for modification. A task's creds
  220. * shouldn't generally be modified directly, therefore this function is used to
  221. * prepare a new copy, which the caller then modifies and then commits by
  222. * calling commit_creds().
  223. *
  224. * Preparation involves making a copy of the objective creds for modification.
  225. *
  226. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  227. *
  228. * Call commit_creds() or abort_creds() to clean up.
  229. */
  230. struct cred *prepare_creds(void)
  231. {
  232. struct task_struct *task = current;
  233. const struct cred *old;
  234. struct cred *new;
  235. validate_process_creds();
  236. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  237. if (!new)
  238. return NULL;
  239. kdebug("prepare_creds() alloc %p", new);
  240. old = task->cred;
  241. memcpy(new, old, sizeof(struct cred));
  242. atomic_set(&new->usage, 1);
  243. set_cred_subscribers(new, 0);
  244. get_group_info(new->group_info);
  245. get_uid(new->user);
  246. #ifdef CONFIG_KEYS
  247. key_get(new->thread_keyring);
  248. key_get(new->request_key_auth);
  249. atomic_inc(&new->tgcred->usage);
  250. #endif
  251. #ifdef CONFIG_SECURITY
  252. new->security = NULL;
  253. #endif
  254. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  255. goto error;
  256. validate_creds(new);
  257. return new;
  258. error:
  259. abort_creds(new);
  260. return NULL;
  261. }
  262. EXPORT_SYMBOL(prepare_creds);
  263. /*
  264. * Prepare credentials for current to perform an execve()
  265. * - The caller must hold current->cred_guard_mutex
  266. */
  267. struct cred *prepare_exec_creds(void)
  268. {
  269. struct thread_group_cred *tgcred = NULL;
  270. struct cred *new;
  271. #ifdef CONFIG_KEYS
  272. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  273. if (!tgcred)
  274. return NULL;
  275. #endif
  276. new = prepare_creds();
  277. if (!new) {
  278. kfree(tgcred);
  279. return new;
  280. }
  281. #ifdef CONFIG_KEYS
  282. /* newly exec'd tasks don't get a thread keyring */
  283. key_put(new->thread_keyring);
  284. new->thread_keyring = NULL;
  285. /* create a new per-thread-group creds for all this set of threads to
  286. * share */
  287. memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
  288. atomic_set(&tgcred->usage, 1);
  289. spin_lock_init(&tgcred->lock);
  290. /* inherit the session keyring; new process keyring */
  291. key_get(tgcred->session_keyring);
  292. tgcred->process_keyring = NULL;
  293. release_tgcred(new);
  294. new->tgcred = tgcred;
  295. #endif
  296. return new;
  297. }
  298. /*
  299. * prepare new credentials for the usermode helper dispatcher
  300. */
  301. struct cred *prepare_usermodehelper_creds(void)
  302. {
  303. #ifdef CONFIG_KEYS
  304. struct thread_group_cred *tgcred = NULL;
  305. #endif
  306. struct cred *new;
  307. #ifdef CONFIG_KEYS
  308. tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
  309. if (!tgcred)
  310. return NULL;
  311. #endif
  312. new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
  313. if (!new)
  314. goto free_tgcred;
  315. kdebug("prepare_usermodehelper_creds() alloc %p", new);
  316. memcpy(new, &init_cred, sizeof(struct cred));
  317. atomic_set(&new->usage, 1);
  318. set_cred_subscribers(new, 0);
  319. get_group_info(new->group_info);
  320. get_uid(new->user);
  321. #ifdef CONFIG_KEYS
  322. new->thread_keyring = NULL;
  323. new->request_key_auth = NULL;
  324. new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
  325. atomic_set(&tgcred->usage, 1);
  326. spin_lock_init(&tgcred->lock);
  327. new->tgcred = tgcred;
  328. #endif
  329. #ifdef CONFIG_SECURITY
  330. new->security = NULL;
  331. #endif
  332. if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
  333. goto error;
  334. validate_creds(new);
  335. BUG_ON(atomic_read(&new->usage) != 1);
  336. return new;
  337. error:
  338. put_cred(new);
  339. return NULL;
  340. free_tgcred:
  341. #ifdef CONFIG_KEYS
  342. kfree(tgcred);
  343. #endif
  344. return NULL;
  345. }
  346. /*
  347. * Copy credentials for the new process created by fork()
  348. *
  349. * We share if we can, but under some circumstances we have to generate a new
  350. * set.
  351. *
  352. * The new process gets the current process's subjective credentials as its
  353. * objective and subjective credentials
  354. */
  355. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  356. {
  357. #ifdef CONFIG_KEYS
  358. struct thread_group_cred *tgcred;
  359. #endif
  360. struct cred *new;
  361. int ret;
  362. mutex_init(&p->cred_guard_mutex);
  363. if (
  364. #ifdef CONFIG_KEYS
  365. !p->cred->thread_keyring &&
  366. #endif
  367. clone_flags & CLONE_THREAD
  368. ) {
  369. p->real_cred = get_cred(p->cred);
  370. get_cred(p->cred);
  371. alter_cred_subscribers(p->cred, 2);
  372. kdebug("share_creds(%p{%d,%d})",
  373. p->cred, atomic_read(&p->cred->usage),
  374. read_cred_subscribers(p->cred));
  375. atomic_inc(&p->cred->user->processes);
  376. return 0;
  377. }
  378. new = prepare_creds();
  379. if (!new)
  380. return -ENOMEM;
  381. if (clone_flags & CLONE_NEWUSER) {
  382. ret = create_user_ns(new);
  383. if (ret < 0)
  384. goto error_put;
  385. }
  386. #ifdef CONFIG_KEYS
  387. /* new threads get their own thread keyrings if their parent already
  388. * had one */
  389. if (new->thread_keyring) {
  390. key_put(new->thread_keyring);
  391. new->thread_keyring = NULL;
  392. if (clone_flags & CLONE_THREAD)
  393. install_thread_keyring_to_cred(new);
  394. }
  395. /* we share the process and session keyrings between all the threads in
  396. * a process - this is slightly icky as we violate COW credentials a
  397. * bit */
  398. if (!(clone_flags & CLONE_THREAD)) {
  399. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  400. if (!tgcred) {
  401. ret = -ENOMEM;
  402. goto error_put;
  403. }
  404. atomic_set(&tgcred->usage, 1);
  405. spin_lock_init(&tgcred->lock);
  406. tgcred->process_keyring = NULL;
  407. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  408. release_tgcred(new);
  409. new->tgcred = tgcred;
  410. }
  411. #endif
  412. atomic_inc(&new->user->processes);
  413. p->cred = p->real_cred = get_cred(new);
  414. alter_cred_subscribers(new, 2);
  415. validate_creds(new);
  416. return 0;
  417. error_put:
  418. put_cred(new);
  419. return ret;
  420. }
  421. /**
  422. * commit_creds - Install new credentials upon the current task
  423. * @new: The credentials to be assigned
  424. *
  425. * Install a new set of credentials to the current task, using RCU to replace
  426. * the old set. Both the objective and the subjective credentials pointers are
  427. * updated. This function may not be called if the subjective credentials are
  428. * in an overridden state.
  429. *
  430. * This function eats the caller's reference to the new credentials.
  431. *
  432. * Always returns 0 thus allowing this function to be tail-called at the end
  433. * of, say, sys_setgid().
  434. */
  435. int commit_creds(struct cred *new)
  436. {
  437. struct task_struct *task = current;
  438. const struct cred *old = task->real_cred;
  439. kdebug("commit_creds(%p{%d,%d})", new,
  440. atomic_read(&new->usage),
  441. read_cred_subscribers(new));
  442. BUG_ON(task->cred != old);
  443. #ifdef CONFIG_DEBUG_CREDENTIALS
  444. BUG_ON(read_cred_subscribers(old) < 2);
  445. validate_creds(old);
  446. validate_creds(new);
  447. #endif
  448. BUG_ON(atomic_read(&new->usage) < 1);
  449. security_commit_creds(new, old);
  450. get_cred(new); /* we will require a ref for the subj creds too */
  451. /* dumpability changes */
  452. if (old->euid != new->euid ||
  453. old->egid != new->egid ||
  454. old->fsuid != new->fsuid ||
  455. old->fsgid != new->fsgid ||
  456. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  457. if (task->mm)
  458. set_dumpable(task->mm, suid_dumpable);
  459. task->pdeath_signal = 0;
  460. smp_wmb();
  461. }
  462. /* alter the thread keyring */
  463. if (new->fsuid != old->fsuid)
  464. key_fsuid_changed(task);
  465. if (new->fsgid != old->fsgid)
  466. key_fsgid_changed(task);
  467. /* do it
  468. * - What if a process setreuid()'s and this brings the
  469. * new uid over his NPROC rlimit? We can check this now
  470. * cheaply with the new uid cache, so if it matters
  471. * we should be checking for it. -DaveM
  472. */
  473. alter_cred_subscribers(new, 2);
  474. if (new->user != old->user)
  475. atomic_inc(&new->user->processes);
  476. rcu_assign_pointer(task->real_cred, new);
  477. rcu_assign_pointer(task->cred, new);
  478. if (new->user != old->user)
  479. atomic_dec(&old->user->processes);
  480. alter_cred_subscribers(old, -2);
  481. sched_switch_user(task);
  482. /* send notifications */
  483. if (new->uid != old->uid ||
  484. new->euid != old->euid ||
  485. new->suid != old->suid ||
  486. new->fsuid != old->fsuid)
  487. proc_id_connector(task, PROC_EVENT_UID);
  488. if (new->gid != old->gid ||
  489. new->egid != old->egid ||
  490. new->sgid != old->sgid ||
  491. new->fsgid != old->fsgid)
  492. proc_id_connector(task, PROC_EVENT_GID);
  493. /* release the old obj and subj refs both */
  494. put_cred(old);
  495. put_cred(old);
  496. return 0;
  497. }
  498. EXPORT_SYMBOL(commit_creds);
  499. /**
  500. * abort_creds - Discard a set of credentials and unlock the current task
  501. * @new: The credentials that were going to be applied
  502. *
  503. * Discard a set of credentials that were under construction and unlock the
  504. * current task.
  505. */
  506. void abort_creds(struct cred *new)
  507. {
  508. kdebug("abort_creds(%p{%d,%d})", new,
  509. atomic_read(&new->usage),
  510. read_cred_subscribers(new));
  511. #ifdef CONFIG_DEBUG_CREDENTIALS
  512. BUG_ON(read_cred_subscribers(new) != 0);
  513. #endif
  514. BUG_ON(atomic_read(&new->usage) < 1);
  515. put_cred(new);
  516. }
  517. EXPORT_SYMBOL(abort_creds);
  518. /**
  519. * override_creds - Override the current process's subjective credentials
  520. * @new: The credentials to be assigned
  521. *
  522. * Install a set of temporary override subjective credentials on the current
  523. * process, returning the old set for later reversion.
  524. */
  525. const struct cred *override_creds(const struct cred *new)
  526. {
  527. const struct cred *old = current->cred;
  528. kdebug("override_creds(%p{%d,%d})", new,
  529. atomic_read(&new->usage),
  530. read_cred_subscribers(new));
  531. validate_creds(old);
  532. validate_creds(new);
  533. get_cred(new);
  534. alter_cred_subscribers(new, 1);
  535. rcu_assign_pointer(current->cred, new);
  536. alter_cred_subscribers(old, -1);
  537. kdebug("override_creds() = %p{%d,%d}", old,
  538. atomic_read(&old->usage),
  539. read_cred_subscribers(old));
  540. return old;
  541. }
  542. EXPORT_SYMBOL(override_creds);
  543. /**
  544. * revert_creds - Revert a temporary subjective credentials override
  545. * @old: The credentials to be restored
  546. *
  547. * Revert a temporary set of override subjective credentials to an old set,
  548. * discarding the override set.
  549. */
  550. void revert_creds(const struct cred *old)
  551. {
  552. const struct cred *override = current->cred;
  553. kdebug("revert_creds(%p{%d,%d})", old,
  554. atomic_read(&old->usage),
  555. read_cred_subscribers(old));
  556. validate_creds(old);
  557. validate_creds(override);
  558. alter_cred_subscribers(old, 1);
  559. rcu_assign_pointer(current->cred, old);
  560. alter_cred_subscribers(override, -1);
  561. put_cred(override);
  562. }
  563. EXPORT_SYMBOL(revert_creds);
  564. /*
  565. * initialise the credentials stuff
  566. */
  567. void __init cred_init(void)
  568. {
  569. /* allocate a slab in which we can store credentials */
  570. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  571. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  572. }
  573. /**
  574. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  575. * @daemon: A userspace daemon to be used as a reference
  576. *
  577. * Prepare a set of credentials for a kernel service. This can then be used to
  578. * override a task's own credentials so that work can be done on behalf of that
  579. * task that requires a different subjective context.
  580. *
  581. * @daemon is used to provide a base for the security record, but can be NULL.
  582. * If @daemon is supplied, then the security data will be derived from that;
  583. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  584. *
  585. * The caller may change these controls afterwards if desired.
  586. *
  587. * Returns the new credentials or NULL if out of memory.
  588. *
  589. * Does not take, and does not return holding current->cred_replace_mutex.
  590. */
  591. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  592. {
  593. const struct cred *old;
  594. struct cred *new;
  595. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  596. if (!new)
  597. return NULL;
  598. kdebug("prepare_kernel_cred() alloc %p", new);
  599. if (daemon)
  600. old = get_task_cred(daemon);
  601. else
  602. old = get_cred(&init_cred);
  603. validate_creds(old);
  604. *new = *old;
  605. get_uid(new->user);
  606. get_group_info(new->group_info);
  607. #ifdef CONFIG_KEYS
  608. atomic_inc(&init_tgcred.usage);
  609. new->tgcred = &init_tgcred;
  610. new->request_key_auth = NULL;
  611. new->thread_keyring = NULL;
  612. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  613. #endif
  614. #ifdef CONFIG_SECURITY
  615. new->security = NULL;
  616. #endif
  617. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  618. goto error;
  619. atomic_set(&new->usage, 1);
  620. set_cred_subscribers(new, 0);
  621. put_cred(old);
  622. validate_creds(new);
  623. return new;
  624. error:
  625. put_cred(new);
  626. put_cred(old);
  627. return NULL;
  628. }
  629. EXPORT_SYMBOL(prepare_kernel_cred);
  630. /**
  631. * set_security_override - Set the security ID in a set of credentials
  632. * @new: The credentials to alter
  633. * @secid: The LSM security ID to set
  634. *
  635. * Set the LSM security ID in a set of credentials so that the subjective
  636. * security is overridden when an alternative set of credentials is used.
  637. */
  638. int set_security_override(struct cred *new, u32 secid)
  639. {
  640. return security_kernel_act_as(new, secid);
  641. }
  642. EXPORT_SYMBOL(set_security_override);
  643. /**
  644. * set_security_override_from_ctx - Set the security ID in a set of credentials
  645. * @new: The credentials to alter
  646. * @secctx: The LSM security context to generate the security ID from.
  647. *
  648. * Set the LSM security ID in a set of credentials so that the subjective
  649. * security is overridden when an alternative set of credentials is used. The
  650. * security ID is specified in string form as a security context to be
  651. * interpreted by the LSM.
  652. */
  653. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  654. {
  655. u32 secid;
  656. int ret;
  657. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  658. if (ret < 0)
  659. return ret;
  660. return set_security_override(new, secid);
  661. }
  662. EXPORT_SYMBOL(set_security_override_from_ctx);
  663. /**
  664. * set_create_files_as - Set the LSM file create context in a set of credentials
  665. * @new: The credentials to alter
  666. * @inode: The inode to take the context from
  667. *
  668. * Change the LSM file creation context in a set of credentials to be the same
  669. * as the object context of the specified inode, so that the new inodes have
  670. * the same MAC context as that inode.
  671. */
  672. int set_create_files_as(struct cred *new, struct inode *inode)
  673. {
  674. new->fsuid = inode->i_uid;
  675. new->fsgid = inode->i_gid;
  676. return security_kernel_create_files_as(new, inode);
  677. }
  678. EXPORT_SYMBOL(set_create_files_as);
  679. #ifdef CONFIG_DEBUG_CREDENTIALS
  680. bool creds_are_invalid(const struct cred *cred)
  681. {
  682. if (cred->magic != CRED_MAGIC)
  683. return true;
  684. #ifdef CONFIG_SECURITY_SELINUX
  685. if (selinux_is_enabled()) {
  686. if ((unsigned long) cred->security < PAGE_SIZE)
  687. return true;
  688. if ((*(u32 *)cred->security & 0xffffff00) ==
  689. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
  690. return true;
  691. }
  692. #endif
  693. return false;
  694. }
  695. EXPORT_SYMBOL(creds_are_invalid);
  696. /*
  697. * dump invalid credentials
  698. */
  699. static void dump_invalid_creds(const struct cred *cred, const char *label,
  700. const struct task_struct *tsk)
  701. {
  702. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  703. label, cred,
  704. cred == &init_cred ? "[init]" : "",
  705. cred == tsk->real_cred ? "[real]" : "",
  706. cred == tsk->cred ? "[eff]" : "");
  707. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  708. cred->magic, cred->put_addr);
  709. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  710. atomic_read(&cred->usage),
  711. read_cred_subscribers(cred));
  712. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  713. cred->uid, cred->euid, cred->suid, cred->fsuid);
  714. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  715. cred->gid, cred->egid, cred->sgid, cred->fsgid);
  716. #ifdef CONFIG_SECURITY
  717. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  718. if ((unsigned long) cred->security >= PAGE_SIZE &&
  719. (((unsigned long) cred->security & 0xffffff00) !=
  720. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  721. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  722. ((u32*)cred->security)[0],
  723. ((u32*)cred->security)[1]);
  724. #endif
  725. }
  726. /*
  727. * report use of invalid credentials
  728. */
  729. void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  730. {
  731. printk(KERN_ERR "CRED: Invalid credentials\n");
  732. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  733. dump_invalid_creds(cred, "Specified", current);
  734. BUG();
  735. }
  736. EXPORT_SYMBOL(__invalid_creds);
  737. /*
  738. * check the credentials on a process
  739. */
  740. void __validate_process_creds(struct task_struct *tsk,
  741. const char *file, unsigned line)
  742. {
  743. if (tsk->cred == tsk->real_cred) {
  744. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  745. creds_are_invalid(tsk->cred)))
  746. goto invalid_creds;
  747. } else {
  748. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  749. read_cred_subscribers(tsk->cred) < 1 ||
  750. creds_are_invalid(tsk->real_cred) ||
  751. creds_are_invalid(tsk->cred)))
  752. goto invalid_creds;
  753. }
  754. return;
  755. invalid_creds:
  756. printk(KERN_ERR "CRED: Invalid process credentials\n");
  757. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  758. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  759. if (tsk->cred != tsk->real_cred)
  760. dump_invalid_creds(tsk->cred, "Effective", tsk);
  761. else
  762. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  763. BUG();
  764. }
  765. EXPORT_SYMBOL(__validate_process_creds);
  766. /*
  767. * check creds for do_exit()
  768. */
  769. void validate_creds_for_do_exit(struct task_struct *tsk)
  770. {
  771. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  772. tsk->real_cred, tsk->cred,
  773. atomic_read(&tsk->cred->usage),
  774. read_cred_subscribers(tsk->cred));
  775. __validate_process_creds(tsk, __FILE__, __LINE__);
  776. }
  777. #endif /* CONFIG_DEBUG_CREDENTIALS */