Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 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 | |
| 10 | #ifndef _LINUX_FSCRYPT_COMMON_H |
| 11 | #define _LINUX_FSCRYPT_COMMON_H |
| 12 | |
| 13 | #include <linux/key.h> |
| 14 | #include <linux/fs.h> |
| 15 | #include <linux/mm.h> |
| 16 | #include <linux/bio.h> |
| 17 | #include <linux/dcache.h> |
| 18 | #include <crypto/skcipher.h> |
| 19 | #include <uapi/linux/fs.h> |
| 20 | |
| 21 | #define FS_CRYPTO_BLOCK_SIZE 16 |
| 22 | |
| 23 | struct fscrypt_info; |
| 24 | |
| 25 | struct fscrypt_ctx { |
| 26 | union { |
| 27 | struct { |
| 28 | struct page *bounce_page; /* Ciphertext page */ |
| 29 | struct page *control_page; /* Original page */ |
| 30 | } w; |
| 31 | struct { |
| 32 | struct bio *bio; |
| 33 | struct work_struct work; |
| 34 | } r; |
| 35 | struct list_head free_list; /* Free list */ |
| 36 | }; |
| 37 | u8 flags; /* Flags */ |
| 38 | }; |
| 39 | |
| 40 | /** |
| 41 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
| 42 | * of the string in little-endian format. |
| 43 | */ |
| 44 | struct fscrypt_symlink_data { |
| 45 | __le16 len; |
| 46 | char encrypted_path[1]; |
| 47 | } __packed; |
| 48 | |
| 49 | /** |
| 50 | * This function is used to calculate the disk space required to |
| 51 | * store a filename of length l in encrypted symlink format. |
| 52 | */ |
| 53 | static inline u32 fscrypt_symlink_data_len(u32 l) |
| 54 | { |
| 55 | if (l < FS_CRYPTO_BLOCK_SIZE) |
| 56 | l = FS_CRYPTO_BLOCK_SIZE; |
| 57 | return (l + sizeof(struct fscrypt_symlink_data) - 1); |
| 58 | } |
| 59 | |
| 60 | struct fscrypt_str { |
| 61 | unsigned char *name; |
| 62 | u32 len; |
| 63 | }; |
| 64 | |
| 65 | struct fscrypt_name { |
| 66 | const struct qstr *usr_fname; |
| 67 | struct fscrypt_str disk_name; |
| 68 | u32 hash; |
| 69 | u32 minor_hash; |
| 70 | struct fscrypt_str crypto_buf; |
| 71 | }; |
| 72 | |
| 73 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| 74 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) |
| 75 | #define fname_name(p) ((p)->disk_name.name) |
| 76 | #define fname_len(p) ((p)->disk_name.len) |
| 77 | |
| 78 | /* |
| 79 | * fscrypt superblock flags |
| 80 | */ |
| 81 | #define FS_CFLG_OWN_PAGES (1U << 1) |
| 82 | |
| 83 | /* |
| 84 | * crypto opertions for filesystems |
| 85 | */ |
| 86 | struct fscrypt_operations { |
| 87 | unsigned int flags; |
| 88 | const char *key_prefix; |
| 89 | int (*get_context)(struct inode *, void *, size_t); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 90 | int (*set_context)(struct inode *, const void *, size_t, void *); |
| 91 | int (*dummy_context)(struct inode *); |
| 92 | bool (*is_encrypted)(struct inode *); |
| 93 | bool (*empty_dir)(struct inode *); |
| 94 | unsigned (*max_namelen)(struct inode *); |
| 95 | }; |
| 96 | |
| 97 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 98 | { |
| 99 | if (inode->i_sb->s_cop->dummy_context && |
| 100 | inode->i_sb->s_cop->dummy_context(inode)) |
| 101 | return true; |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | static inline bool fscrypt_valid_contents_enc_mode(u32 mode) |
| 106 | { |
| 107 | return (mode == FS_ENCRYPTION_MODE_AES_256_XTS); |
| 108 | } |
| 109 | |
| 110 | static inline bool fscrypt_valid_filenames_enc_mode(u32 mode) |
| 111 | { |
| 112 | return (mode == FS_ENCRYPTION_MODE_AES_256_CTS); |
| 113 | } |
| 114 | |
| 115 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
| 116 | { |
| 117 | if (str->len == 1 && str->name[0] == '.') |
| 118 | return true; |
| 119 | |
| 120 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') |
| 121 | return true; |
| 122 | |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | static inline struct page *fscrypt_control_page(struct page *page) |
| 127 | { |
| 128 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 129 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; |
| 130 | #else |
| 131 | WARN_ON_ONCE(1); |
| 132 | return ERR_PTR(-EINVAL); |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | static inline int fscrypt_has_encryption_key(const struct inode *inode) |
| 137 | { |
| 138 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) |
| 139 | return (inode->i_crypt_info != NULL); |
| 140 | #else |
| 141 | return 0; |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | #endif /* _LINUX_FSCRYPT_COMMON_H */ |