fscrypt_common.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  43. * This function is used to calculate the disk space required to
  44. * store a filename of length l in encrypted symlink format.
  45. */
  46. static inline u32 fscrypt_symlink_data_len(u32 l)
  47. {
  48. if (l < FS_CRYPTO_BLOCK_SIZE)
  49. l = FS_CRYPTO_BLOCK_SIZE;
  50. return (l + sizeof(struct fscrypt_symlink_data) - 1);
  51. }
  52. struct fscrypt_str {
  53. unsigned char *name;
  54. u32 len;
  55. };
  56. struct fscrypt_name {
  57. const struct qstr *usr_fname;
  58. struct fscrypt_str disk_name;
  59. u32 hash;
  60. u32 minor_hash;
  61. struct fscrypt_str crypto_buf;
  62. };
  63. #define FSTR_INIT(n, l) { .name = n, .len = l }
  64. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  65. #define fname_name(p) ((p)->disk_name.name)
  66. #define fname_len(p) ((p)->disk_name.len)
  67. /*
  68. * fscrypt superblock flags
  69. */
  70. #define FS_CFLG_OWN_PAGES (1U << 1)
  71. /*
  72. * crypto opertions for filesystems
  73. */
  74. struct fscrypt_operations {
  75. unsigned int flags;
  76. const char *key_prefix;
  77. int (*get_context)(struct inode *, void *, size_t);
  78. int (*prepare_context)(struct inode *);
  79. int (*set_context)(struct inode *, const void *, size_t, void *);
  80. int (*dummy_context)(struct inode *);
  81. bool (*is_encrypted)(struct inode *);
  82. bool (*empty_dir)(struct inode *);
  83. unsigned (*max_namelen)(struct inode *);
  84. };
  85. static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
  86. {
  87. if (inode->i_sb->s_cop->dummy_context &&
  88. inode->i_sb->s_cop->dummy_context(inode))
  89. return true;
  90. return false;
  91. }
  92. static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
  93. {
  94. return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
  95. }
  96. static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
  97. {
  98. return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
  99. }
  100. static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
  101. {
  102. if (str->len == 1 && str->name[0] == '.')
  103. return true;
  104. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  105. return true;
  106. return false;
  107. }
  108. static inline struct page *fscrypt_control_page(struct page *page)
  109. {
  110. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  111. return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
  112. #else
  113. WARN_ON_ONCE(1);
  114. return ERR_PTR(-EINVAL);
  115. #endif
  116. }
  117. static inline int fscrypt_has_encryption_key(const struct inode *inode)
  118. {
  119. #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
  120. return (inode->i_crypt_info != NULL);
  121. #else
  122. return 0;
  123. #endif
  124. }
  125. #endif /* _LINUX_FSCRYPT_COMMON_H */