Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 1 | /* |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 2 | * fscrypt.h: declarations for per-file encryption |
| 3 | * |
| 4 | * Filesystems that implement per-file encryption include this header |
| 5 | * file with the __FS_HAS_ENCRYPTION set according to whether that filesystem |
| 6 | * is being built with encryption support or not. |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 2015, Google, Inc. |
| 9 | * |
| 10 | * Written by Michael Halcrow, 2015. |
| 11 | * Modified by Jaegeuk Kim, 2015. |
| 12 | */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 13 | #ifndef _LINUX_FSCRYPT_H |
| 14 | #define _LINUX_FSCRYPT_H |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 15 | |
| 16 | #include <linux/key.h> |
| 17 | #include <linux/fs.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/bio.h> |
| 20 | #include <linux/dcache.h> |
| 21 | #include <crypto/skcipher.h> |
| 22 | #include <uapi/linux/fs.h> |
| 23 | |
| 24 | #define FS_CRYPTO_BLOCK_SIZE 16 |
| 25 | |
| 26 | struct fscrypt_info; |
| 27 | |
| 28 | struct fscrypt_ctx { |
| 29 | union { |
| 30 | struct { |
| 31 | struct page *bounce_page; /* Ciphertext page */ |
| 32 | struct page *control_page; /* Original page */ |
| 33 | } w; |
| 34 | struct { |
| 35 | struct bio *bio; |
| 36 | struct work_struct work; |
| 37 | } r; |
| 38 | struct list_head free_list; /* Free list */ |
| 39 | }; |
| 40 | u8 flags; /* Flags */ |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * For encrypted symlinks, the ciphertext length is stored at the beginning |
| 45 | * of the string in little-endian format. |
| 46 | */ |
| 47 | struct fscrypt_symlink_data { |
| 48 | __le16 len; |
| 49 | char encrypted_path[1]; |
| 50 | } __packed; |
| 51 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 52 | struct fscrypt_str { |
| 53 | unsigned char *name; |
| 54 | u32 len; |
| 55 | }; |
| 56 | |
| 57 | struct fscrypt_name { |
| 58 | const struct qstr *usr_fname; |
| 59 | struct fscrypt_str disk_name; |
| 60 | u32 hash; |
| 61 | u32 minor_hash; |
| 62 | struct fscrypt_str crypto_buf; |
| 63 | }; |
| 64 | |
| 65 | #define FSTR_INIT(n, l) { .name = n, .len = l } |
| 66 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) |
| 67 | #define fname_name(p) ((p)->disk_name.name) |
| 68 | #define fname_len(p) ((p)->disk_name.len) |
| 69 | |
| 70 | /* |
| 71 | * fscrypt superblock flags |
| 72 | */ |
| 73 | #define FS_CFLG_OWN_PAGES (1U << 1) |
| 74 | |
| 75 | /* |
| 76 | * crypto opertions for filesystems |
| 77 | */ |
| 78 | struct fscrypt_operations { |
| 79 | unsigned int flags; |
| 80 | const char *key_prefix; |
| 81 | int (*get_context)(struct inode *, void *, size_t); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 82 | int (*set_context)(struct inode *, const void *, size_t, void *); |
Eric Biggers | c250b7d | 2017-06-22 12:14:40 -0700 | [diff] [blame] | 83 | bool (*dummy_context)(struct inode *); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 84 | bool (*is_encrypted)(struct inode *); |
| 85 | bool (*empty_dir)(struct inode *); |
| 86 | unsigned (*max_namelen)(struct inode *); |
| 87 | }; |
| 88 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 89 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ |
| 90 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 |
| 91 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 92 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) |
| 93 | { |
| 94 | if (inode->i_sb->s_cop->dummy_context && |
| 95 | inode->i_sb->s_cop->dummy_context(inode)) |
| 96 | return true; |
| 97 | return false; |
| 98 | } |
| 99 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 100 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, |
| 101 | u32 filenames_mode) |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 102 | { |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 103 | if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC && |
| 104 | filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS) |
| 105 | return true; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 106 | |
Daniel Walter | b7e7cf7 | 2017-06-19 09:27:58 +0200 | [diff] [blame] | 107 | if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS && |
| 108 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) |
| 109 | return true; |
| 110 | |
| 111 | return false; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) |
| 115 | { |
| 116 | if (str->len == 1 && str->name[0] == '.') |
| 117 | return true; |
| 118 | |
| 119 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') |
| 120 | return true; |
| 121 | |
| 122 | return false; |
| 123 | } |
| 124 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 125 | #if __FS_HAS_ENCRYPTION |
| 126 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 127 | static inline struct page *fscrypt_control_page(struct page *page) |
| 128 | { |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 129 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 130 | } |
| 131 | |
| 132 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
| 133 | { |
| 134 | return (inode->i_crypt_info != NULL); |
| 135 | } |
| 136 | |
| 137 | #include <linux/fscrypt_supp.h> |
| 138 | |
| 139 | #else /* !__FS_HAS_ENCRYPTION */ |
| 140 | |
| 141 | static inline struct page *fscrypt_control_page(struct page *page) |
| 142 | { |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 143 | WARN_ON_ONCE(1); |
| 144 | return ERR_PTR(-EINVAL); |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 147 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 148 | { |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 149 | return 0; |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame^] | 152 | #include <linux/fscrypt_notsupp.h> |
| 153 | #endif /* __FS_HAS_ENCRYPTION */ |
| 154 | |
| 155 | |
| 156 | #endif /* _LINUX_FSCRYPT_H */ |