fscrypt_common.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * fscrypt_common.h: common declarations for per-file encryption
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * Written by Michael Halcrow, 2015.
  7. * Modified by Jaegeuk Kim, 2015.
  8. */
  9. #ifndef _LINUX_FSCRYPT_COMMON_H
  10. #define _LINUX_FSCRYPT_COMMON_H
  11. #include <linux/key.h>
  12. #include <linux/fs.h>
  13. #include <linux/mm.h>
  14. #include <linux/bio.h>
  15. #include <linux/dcache.h>
  16. #include <crypto/skcipher.h>
  17. #include <uapi/linux/fs.h>
  18. #define FS_CRYPTO_BLOCK_SIZE 16
  19. struct fscrypt_info;
  20. struct fscrypt_ctx {
  21. union {
  22. struct {
  23. struct page *bounce_page; /* Ciphertext page */
  24. struct page *control_page; /* Original page */
  25. } w;
  26. struct {
  27. struct bio *bio;
  28. struct work_struct work;
  29. } r;
  30. struct list_head free_list; /* Free list */
  31. };
  32. u8 flags; /* Flags */
  33. };
  34. /**
  35. * For encrypted symlinks, the ciphertext length is stored at the beginning
  36. * of the string in little-endian format.
  37. */
  38. struct fscrypt_symlink_data {
  39. __le16 len;
  40. char encrypted_path[1];
  41. } __packed;
  42. struct fscrypt_str {
  43. unsigned char *name;
  44. u32 len;
  45. };
  46. struct fscrypt_name {
  47. const struct qstr *usr_fname;
  48. struct fscrypt_str disk_name;
  49. u32 hash;
  50. u32 minor_hash;
  51. struct fscrypt_str crypto_buf;
  52. };
  53. #define FSTR_INIT(n, l) { .name = n, .len = l }
  54. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  55. #define fname_name(p) ((p)->disk_name.name)
  56. #define fname_len(p) ((p)->disk_name.len)
  57. /*
  58. * fscrypt superblock flags
  59. */
  60. #define FS_CFLG_OWN_PAGES (1U << 1)
  61. /*
  62. * crypto opertions for filesystems
  63. */
  64. struct fscrypt_operations {
  65. unsigned int flags;
  66. const char *key_prefix;
  67. int (*get_context)(struct inode *, void *, size_t);
  68. int (*set_context)(struct inode *, const void *, size_t, void *);
  69. int (*dummy_context)(struct inode *);
  70. bool (*is_encrypted)(struct inode *);
  71. bool (*empty_dir)(struct inode *);
  72. unsigned (*max_namelen)(struct inode *);
  73. };
  74. static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
  75. {
  76. if (inode->i_sb->s_cop->dummy_context &&
  77. inode->i_sb->s_cop->dummy_context(inode))
  78. return true;
  79. return false;
  80. }
  81. static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
  82. u32 filenames_mode)
  83. {
  84. if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC &&
  85. filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS)
  86. return true;
  87. if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS &&
  88. filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
  89. return true;
  90. return false;
  91. }
  92. static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
  93. {
  94. if (str->len == 1 && str->name[0] == '.')
  95. return true;
  96. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  97. return true;
  98. return false;
  99. }
  100. static inline struct page *fscrypt_control_page(struct page *page)
  101. {
  102. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  103. return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
  104. #else
  105. WARN_ON_ONCE(1);
  106. return ERR_PTR(-EINVAL);
  107. #endif
  108. }
  109. static inline int fscrypt_has_encryption_key(const struct inode *inode)
  110. {
  111. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  112. return (inode->i_crypt_info != NULL);
  113. #else
  114. return 0;
  115. #endif
  116. }
  117. #endif /* _LINUX_FSCRYPT_COMMON_H */