cred.c 22 KB

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