cred.c 22 KB

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