environ.c 3.0 KB

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