file.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * AppArmor security module
  4. *
  5. * This file contains AppArmor file mediation function definitions.
  6. *
  7. * Copyright (C) 1998-2008 Novell/SUSE
  8. * Copyright 2009-2010 Canonical Ltd.
  9. */
  10. #ifndef __AA_FILE_H
  11. #define __AA_FILE_H
  12. #include <linux/spinlock.h>
  13. #include "domain.h"
  14. #include "match.h"
  15. #include "perms.h"
  16. struct aa_policydb;
  17. struct aa_profile;
  18. struct path;
  19. #define mask_mode_t(X) (X & (MAY_EXEC | MAY_WRITE | MAY_READ | MAY_APPEND))
  20. #define AA_AUDIT_FILE_MASK (MAY_READ | MAY_WRITE | MAY_EXEC | MAY_APPEND |\
  21. AA_MAY_CREATE | AA_MAY_DELETE | \
  22. AA_MAY_GETATTR | AA_MAY_SETATTR | \
  23. AA_MAY_CHMOD | AA_MAY_CHOWN | AA_MAY_LOCK | \
  24. AA_EXEC_MMAP | AA_MAY_LINK)
  25. static inline struct aa_file_ctx *file_ctx(struct file *file)
  26. {
  27. return file->f_security + apparmor_blob_sizes.lbs_file;
  28. }
  29. /* struct aa_file_ctx - the AppArmor context the file was opened in
  30. * @lock: lock to update the ctx
  31. * @label: label currently cached on the ctx
  32. * @perms: the permission the file was opened with
  33. */
  34. struct aa_file_ctx {
  35. spinlock_t lock;
  36. struct aa_label __rcu *label;
  37. u32 allow;
  38. };
  39. /*
  40. * The xindex is broken into 3 parts
  41. * - index - an index into either the exec name table or the variable table
  42. * - exec type - which determines how the executable name and index are used
  43. * - flags - which modify how the destination name is applied
  44. */
  45. #define AA_X_INDEX_MASK AA_INDEX_MASK
  46. #define AA_X_TYPE_MASK 0x0c000000
  47. #define AA_X_NONE AA_INDEX_NONE
  48. #define AA_X_NAME 0x04000000 /* use executable name px */
  49. #define AA_X_TABLE 0x08000000 /* use a specified name ->n# */
  50. #define AA_X_UNSAFE 0x10000000
  51. #define AA_X_CHILD 0x20000000
  52. #define AA_X_INHERIT 0x40000000
  53. #define AA_X_UNCONFINED 0x80000000
  54. /* need to make conditional which ones are being set */
  55. struct path_cond {
  56. kuid_t uid;
  57. umode_t mode;
  58. };
  59. #define COMBINED_PERM_MASK(X) ((X).allow | (X).audit | (X).quiet | (X).kill)
  60. int aa_audit_file(const struct cred *cred,
  61. struct aa_profile *profile, struct aa_perms *perms,
  62. const char *op, u32 request, const char *name,
  63. const char *target, struct aa_label *tlabel, kuid_t ouid,
  64. const char *info, int error);
  65. struct aa_perms *aa_lookup_fperms(struct aa_policydb *file_rules,
  66. aa_state_t state, struct path_cond *cond);
  67. aa_state_t aa_str_perms(struct aa_policydb *file_rules, aa_state_t start,
  68. const char *name, struct path_cond *cond,
  69. struct aa_perms *perms);
  70. int aa_path_perm(const char *op, const struct cred *subj_cred,
  71. struct aa_label *label, const struct path *path,
  72. int flags, u32 request, struct path_cond *cond);
  73. int aa_path_link(const struct cred *subj_cred, struct aa_label *label,
  74. struct dentry *old_dentry, const struct path *new_dir,
  75. struct dentry *new_dentry);
  76. int aa_file_perm(const char *op, const struct cred *subj_cred,
  77. struct aa_label *label, struct file *file,
  78. u32 request, bool in_atomic);
  79. void aa_inherit_files(const struct cred *cred, struct files_struct *files);
  80. /**
  81. * aa_map_file_perms - map file flags to AppArmor permissions
  82. * @file: open file to map flags to AppArmor permissions
  83. *
  84. * Returns: apparmor permission set for the file
  85. */
  86. static inline u32 aa_map_file_to_perms(struct file *file)
  87. {
  88. int flags = file->f_flags;
  89. u32 perms = 0;
  90. if (file->f_mode & FMODE_WRITE)
  91. perms |= MAY_WRITE;
  92. if (file->f_mode & FMODE_READ)
  93. perms |= MAY_READ;
  94. if ((flags & O_APPEND) && (perms & MAY_WRITE))
  95. perms = (perms & ~MAY_WRITE) | MAY_APPEND;
  96. /* trunc implies write permission */
  97. if (flags & O_TRUNC)
  98. perms |= MAY_WRITE;
  99. if (flags & O_CREAT)
  100. perms |= AA_MAY_CREATE;
  101. return perms;
  102. }
  103. #endif /* __AA_FILE_H */