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