cred.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Landlock - Credential hooks
  4. *
  5. * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2018-2020 ANSSI
  7. * Copyright © 2024-2025 Microsoft Corporation
  8. */
  9. #include <linux/binfmts.h>
  10. #include <linux/cred.h>
  11. #include <linux/lsm_hooks.h>
  12. #include "common.h"
  13. #include "cred.h"
  14. #include "ruleset.h"
  15. #include "setup.h"
  16. static void hook_cred_transfer(struct cred *const new,
  17. const struct cred *const old)
  18. {
  19. const struct landlock_cred_security *const old_llcred =
  20. landlock_cred(old);
  21. if (old_llcred->domain) {
  22. landlock_get_ruleset(old_llcred->domain);
  23. *landlock_cred(new) = *old_llcred;
  24. }
  25. }
  26. static int hook_cred_prepare(struct cred *const new,
  27. const struct cred *const old, const gfp_t gfp)
  28. {
  29. hook_cred_transfer(new, old);
  30. return 0;
  31. }
  32. static void hook_cred_free(struct cred *const cred)
  33. {
  34. struct landlock_ruleset *const dom = landlock_cred(cred)->domain;
  35. if (dom)
  36. landlock_put_ruleset_deferred(dom);
  37. }
  38. #ifdef CONFIG_AUDIT
  39. static int hook_bprm_creds_for_exec(struct linux_binprm *const bprm)
  40. {
  41. /* Resets for each execution. */
  42. landlock_cred(bprm->cred)->domain_exec = 0;
  43. return 0;
  44. }
  45. #endif /* CONFIG_AUDIT */
  46. static struct security_hook_list landlock_hooks[] __ro_after_init = {
  47. LSM_HOOK_INIT(cred_prepare, hook_cred_prepare),
  48. LSM_HOOK_INIT(cred_transfer, hook_cred_transfer),
  49. LSM_HOOK_INIT(cred_free, hook_cred_free),
  50. #ifdef CONFIG_AUDIT
  51. LSM_HOOK_INIT(bprm_creds_for_exec, hook_bprm_creds_for_exec),
  52. #endif /* CONFIG_AUDIT */
  53. };
  54. __init void landlock_add_cred_hooks(void)
  55. {
  56. security_add_hooks(landlock_hooks, ARRAY_SIZE(landlock_hooks),
  57. &landlock_lsmid);
  58. }