cred.c 22 KB

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