cred.c 22 KB

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