cred.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Task credentials management
  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/sched.h>
  14. #include <linux/key.h>
  15. #include <linux/keyctl.h>
  16. #include <linux/init_task.h>
  17. #include <linux/security.h>
  18. /*
  19. * The initial credentials for the initial task
  20. */
  21. struct cred init_cred = {
  22. .usage = ATOMIC_INIT(3),
  23. .securebits = SECUREBITS_DEFAULT,
  24. .cap_inheritable = CAP_INIT_INH_SET,
  25. .cap_permitted = CAP_FULL_SET,
  26. .cap_effective = CAP_INIT_EFF_SET,
  27. .cap_bset = CAP_INIT_BSET,
  28. .user = INIT_USER,
  29. .group_info = &init_groups,
  30. };
  31. /*
  32. * The RCU callback to actually dispose of a set of credentials
  33. */
  34. static void put_cred_rcu(struct rcu_head *rcu)
  35. {
  36. struct cred *cred = container_of(rcu, struct cred, rcu);
  37. BUG_ON(atomic_read(&cred->usage) != 0);
  38. key_put(cred->thread_keyring);
  39. key_put(cred->request_key_auth);
  40. put_group_info(cred->group_info);
  41. free_uid(cred->user);
  42. security_cred_free(cred);
  43. kfree(cred);
  44. }
  45. /**
  46. * __put_cred - Destroy a set of credentials
  47. * @sec: The record to release
  48. *
  49. * Destroy a set of credentials on which no references remain.
  50. */
  51. void __put_cred(struct cred *cred)
  52. {
  53. call_rcu(&cred->rcu, put_cred_rcu);
  54. }
  55. EXPORT_SYMBOL(__put_cred);
  56. /*
  57. * Copy credentials for the new process created by fork()
  58. */
  59. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  60. {
  61. struct cred *pcred;
  62. int ret;
  63. pcred = kmemdup(p->cred, sizeof(*p->cred), GFP_KERNEL);
  64. if (!pcred)
  65. return -ENOMEM;
  66. #ifdef CONFIG_SECURITY
  67. pcred->security = NULL;
  68. #endif
  69. ret = security_cred_alloc(pcred);
  70. if (ret < 0) {
  71. kfree(pcred);
  72. return ret;
  73. }
  74. atomic_set(&pcred->usage, 1);
  75. get_group_info(pcred->group_info);
  76. get_uid(pcred->user);
  77. key_get(pcred->thread_keyring);
  78. key_get(pcred->request_key_auth);
  79. atomic_inc(&pcred->user->processes);
  80. /* RCU assignment is unneeded here as no-one can have accessed this
  81. * pointer yet, barring us */
  82. p->cred = pcred;
  83. return 0;
  84. }