Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fscrypt_supp.h |
| 3 | * |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 4 | * Do not include this file directly. Use fscrypt.h instead! |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 5 | */ |
Dave Chinner | 734f0d2 | 2017-10-09 12:15:34 -0700 | [diff] [blame] | 6 | #ifndef _LINUX_FSCRYPT_H |
| 7 | #error "Incorrect include of linux/fscrypt_supp.h!" |
| 8 | #endif |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 9 | |
| 10 | #ifndef _LINUX_FSCRYPT_SUPP_H |
| 11 | #define _LINUX_FSCRYPT_SUPP_H |
| 12 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 13 | /* crypto.c */ |
| 14 | extern struct kmem_cache *fscrypt_info_cachep; |
| 15 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); |
| 16 | extern void fscrypt_release_ctx(struct fscrypt_ctx *); |
| 17 | extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *, |
| 18 | unsigned int, unsigned int, |
| 19 | u64, gfp_t); |
| 20 | extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int, |
| 21 | unsigned int, u64); |
| 22 | extern void fscrypt_restore_control_page(struct page *); |
| 23 | |
| 24 | extern const struct dentry_operations fscrypt_d_ops; |
| 25 | |
| 26 | static inline void fscrypt_set_d_op(struct dentry *dentry) |
| 27 | { |
| 28 | d_set_d_op(dentry, &fscrypt_d_ops); |
| 29 | } |
| 30 | |
| 31 | static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry) |
| 32 | { |
| 33 | spin_lock(&dentry->d_lock); |
| 34 | dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY; |
| 35 | spin_unlock(&dentry->d_lock); |
| 36 | } |
| 37 | |
| 38 | /* policy.c */ |
| 39 | extern int fscrypt_ioctl_set_policy(struct file *, const void __user *); |
| 40 | extern int fscrypt_ioctl_get_policy(struct file *, void __user *); |
| 41 | extern int fscrypt_has_permitted_context(struct inode *, struct inode *); |
| 42 | extern int fscrypt_inherit_context(struct inode *, struct inode *, |
| 43 | void *, bool); |
| 44 | /* keyinfo.c */ |
| 45 | extern int fscrypt_get_encryption_info(struct inode *); |
| 46 | extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *); |
| 47 | |
| 48 | /* fname.c */ |
| 49 | extern int fscrypt_setup_filename(struct inode *, const struct qstr *, |
| 50 | int lookup, struct fscrypt_name *); |
Eric Biggers | 27e47a6 | 2017-05-22 18:14:06 -0700 | [diff] [blame] | 51 | |
| 52 | static inline void fscrypt_free_filename(struct fscrypt_name *fname) |
| 53 | { |
| 54 | kfree(fname->crypto_buf.name); |
| 55 | } |
| 56 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 57 | extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32); |
| 58 | extern int fscrypt_fname_alloc_buffer(const struct inode *, u32, |
| 59 | struct fscrypt_str *); |
| 60 | extern void fscrypt_fname_free_buffer(struct fscrypt_str *); |
| 61 | extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32, |
| 62 | const struct fscrypt_str *, struct fscrypt_str *); |
| 63 | extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *, |
| 64 | struct fscrypt_str *); |
| 65 | |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 66 | #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32 |
| 67 | |
| 68 | /* Extracts the second-to-last ciphertext block; see explanation below */ |
| 69 | #define FSCRYPT_FNAME_DIGEST(name, len) \ |
| 70 | ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \ |
| 71 | FS_CRYPTO_BLOCK_SIZE)) |
| 72 | |
| 73 | #define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE |
| 74 | |
| 75 | /** |
| 76 | * fscrypt_digested_name - alternate identifier for an on-disk filename |
| 77 | * |
| 78 | * When userspace lists an encrypted directory without access to the key, |
| 79 | * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE |
| 80 | * bytes are shown in this abbreviated form (base64-encoded) rather than as the |
| 81 | * full ciphertext (base64-encoded). This is necessary to allow supporting |
| 82 | * filenames up to NAME_MAX bytes, since base64 encoding expands the length. |
| 83 | * |
| 84 | * To make it possible for filesystems to still find the correct directory entry |
| 85 | * despite not knowing the full on-disk name, we encode any filesystem-specific |
| 86 | * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups, |
| 87 | * followed by the second-to-last ciphertext block of the filename. Due to the |
| 88 | * use of the CBC-CTS encryption mode, the second-to-last ciphertext block |
| 89 | * depends on the full plaintext. (Note that ciphertext stealing causes the |
Eric Biggers | 6f9d696 | 2017-05-01 11:43:32 -0700 | [diff] [blame] | 90 | * last two blocks to appear "flipped".) This makes accidental collisions very |
| 91 | * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they |
| 92 | * share the same filesystem-specific hashes. |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 93 | * |
Eric Biggers | 6f9d696 | 2017-05-01 11:43:32 -0700 | [diff] [blame] | 94 | * However, this scheme isn't immune to intentional collisions, which can be |
| 95 | * created by anyone able to create arbitrary plaintext filenames and view them |
| 96 | * without the key. Making the "digest" be a real cryptographic hash like |
| 97 | * SHA-256 over the full ciphertext would prevent this, although it would be |
| 98 | * less efficient and harder to implement, especially since the filesystem would |
| 99 | * need to calculate it for each directory entry examined during a search. |
Eric Biggers | 1715942 | 2017-04-24 10:00:10 -0700 | [diff] [blame] | 100 | */ |
| 101 | struct fscrypt_digested_name { |
| 102 | u32 hash; |
| 103 | u32 minor_hash; |
| 104 | u8 digest[FSCRYPT_FNAME_DIGEST_SIZE]; |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * fscrypt_match_name() - test whether the given name matches a directory entry |
| 109 | * @fname: the name being searched for |
| 110 | * @de_name: the name from the directory entry |
| 111 | * @de_name_len: the length of @de_name in bytes |
| 112 | * |
| 113 | * Normally @fname->disk_name will be set, and in that case we simply compare |
| 114 | * that to the name stored in the directory entry. The only exception is that |
| 115 | * if we don't have the key for an encrypted directory and a filename in it is |
| 116 | * very long, then we won't have the full disk_name and we'll instead need to |
| 117 | * match against the fscrypt_digested_name. |
| 118 | * |
| 119 | * Return: %true if the name matches, otherwise %false. |
| 120 | */ |
| 121 | static inline bool fscrypt_match_name(const struct fscrypt_name *fname, |
| 122 | const u8 *de_name, u32 de_name_len) |
| 123 | { |
| 124 | if (unlikely(!fname->disk_name.name)) { |
| 125 | const struct fscrypt_digested_name *n = |
| 126 | (const void *)fname->crypto_buf.name; |
| 127 | if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_')) |
| 128 | return false; |
| 129 | if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) |
| 130 | return false; |
| 131 | return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len), |
| 132 | n->digest, FSCRYPT_FNAME_DIGEST_SIZE); |
| 133 | } |
| 134 | |
| 135 | if (de_name_len != fname->disk_name.len) |
| 136 | return false; |
| 137 | return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len); |
| 138 | } |
| 139 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 140 | /* bio.c */ |
| 141 | extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *); |
| 142 | extern void fscrypt_pullback_bio_page(struct page **, bool); |
| 143 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, |
| 144 | unsigned int); |
| 145 | |
Eric Biggers | efcc7ae | 2017-10-09 12:15:40 -0700 | [diff] [blame] | 146 | /* hooks.c */ |
| 147 | extern int fscrypt_file_open(struct inode *inode, struct file *filp); |
Eric Biggers | 0ea87a9 | 2017-10-09 12:15:41 -0700 | [diff] [blame^] | 148 | extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir); |
Eric Biggers | efcc7ae | 2017-10-09 12:15:40 -0700 | [diff] [blame] | 149 | |
Eric Biggers | 46f47e4 | 2017-01-24 10:58:06 -0800 | [diff] [blame] | 150 | #endif /* _LINUX_FSCRYPT_SUPP_H */ |