environ.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * security/tomoyo/environ.c
  4. *
  5. * Copyright (C) 2005-2011 NTT DATA CORPORATION
  6. */
  7. #include "common.h"
  8. /**
  9. * tomoyo_check_env_acl - Check permission for environment variable's name.
  10. *
  11. * @r: Pointer to "struct tomoyo_request_info".
  12. * @ptr: Pointer to "struct tomoyo_acl_info".
  13. *
  14. * Returns true if granted, false otherwise.
  15. */
  16. static bool tomoyo_check_env_acl(struct tomoyo_request_info *r,
  17. const struct tomoyo_acl_info *ptr)
  18. {
  19. const struct tomoyo_env_acl *acl =
  20. container_of(ptr, typeof(*acl), head);
  21. return tomoyo_path_matches_pattern(r->param.environ.name, acl->env);
  22. }
  23. /**
  24. * tomoyo_audit_env_log - Audit environment variable name log.
  25. *
  26. * @r: Pointer to "struct tomoyo_request_info".
  27. *
  28. * Returns 0 on success, negative value otherwise.
  29. */
  30. static int tomoyo_audit_env_log(struct tomoyo_request_info *r)
  31. {
  32. return tomoyo_supervisor(r, "misc env %s\n",
  33. r->param.environ.name->name);
  34. }
  35. /**
  36. * tomoyo_env_perm - Check permission for environment variable's name.
  37. *
  38. * @r: Pointer to "struct tomoyo_request_info".
  39. * @env: The name of environment variable.
  40. *
  41. * Returns 0 on success, negative value otherwise.
  42. *
  43. * Caller holds tomoyo_read_lock().
  44. */
  45. int tomoyo_env_perm(struct tomoyo_request_info *r, const char *env)
  46. {
  47. struct tomoyo_path_info environ;
  48. int error;
  49. if (!env || !*env)
  50. return 0;
  51. environ.name = env;
  52. tomoyo_fill_path_info(&environ);
  53. r->param_type = TOMOYO_TYPE_ENV_ACL;
  54. r->param.environ.name = &environ;
  55. do {
  56. tomoyo_check_acl(r, tomoyo_check_env_acl);
  57. error = tomoyo_audit_env_log(r);
  58. } while (error == TOMOYO_RETRY_REQUEST);
  59. return error;
  60. }
  61. /**
  62. * tomoyo_same_env_acl - Check for duplicated "struct tomoyo_env_acl" entry.
  63. *
  64. * @a: Pointer to "struct tomoyo_acl_info".
  65. * @b: Pointer to "struct tomoyo_acl_info".
  66. *
  67. * Returns true if @a == @b, false otherwise.
  68. */
  69. static bool tomoyo_same_env_acl(const struct tomoyo_acl_info *a,
  70. const struct tomoyo_acl_info *b)
  71. {
  72. const struct tomoyo_env_acl *p1 = container_of(a, typeof(*p1), head);
  73. const struct tomoyo_env_acl *p2 = container_of(b, typeof(*p2), head);
  74. return p1->env == p2->env;
  75. }
  76. /**
  77. * tomoyo_write_env - Write "struct tomoyo_env_acl" list.
  78. *
  79. * @param: Pointer to "struct tomoyo_acl_param".
  80. *
  81. * Returns 0 on success, negative value otherwise.
  82. *
  83. * Caller holds tomoyo_read_lock().
  84. */
  85. static int tomoyo_write_env(struct tomoyo_acl_param *param)
  86. {
  87. struct tomoyo_env_acl e = { .head.type = TOMOYO_TYPE_ENV_ACL };
  88. int error = -ENOMEM;
  89. const char *data = tomoyo_read_token(param);
  90. if (!tomoyo_correct_word(data) || strchr(data, '='))
  91. return -EINVAL;
  92. e.env = tomoyo_get_name(data);
  93. if (!e.env)
  94. return error;
  95. error = tomoyo_update_domain(&e.head, sizeof(e), param,
  96. tomoyo_same_env_acl, NULL);
  97. tomoyo_put_name(e.env);
  98. return error;
  99. }
  100. /**
  101. * tomoyo_write_misc - Update environment variable list.
  102. *
  103. * @param: Pointer to "struct tomoyo_acl_param".
  104. *
  105. * Returns 0 on success, negative value otherwise.
  106. */
  107. int tomoyo_write_misc(struct tomoyo_acl_param *param)
  108. {
  109. if (tomoyo_str_starts(&param->data, "env "))
  110. return tomoyo_write_env(param);
  111. return -EINVAL;
  112. }