blob: 99b6c52a305cdafdd4558f10c8c9fefa06e545a5 [file] [log] [blame]
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07001/*
2 * fscrypt_supp.h
3 *
Dave Chinner16179292017-10-09 12:15:34 -07004 * Do not include this file directly. Use fscrypt.h instead!
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07005 */
Dave Chinner16179292017-10-09 12:15:34 -07006#ifndef _LINUX_FSCRYPT_H
7#error "Incorrect include of linux/fscrypt_supp.h!"
8#endif
Jaegeuk Kime6b120d2017-07-10 12:55:09 -07009
10#ifndef _LINUX_FSCRYPT_SUPP_H
11#define _LINUX_FSCRYPT_SUPP_H
12
Eric Biggerseb9c5fd2018-01-05 10:45:00 -080013#include <linux/mm.h>
14#include <linux/slab.h>
15
Eric Biggersefbfa8c2018-01-05 10:44:57 -080016/*
17 * fscrypt superblock flags
18 */
19#define FS_CFLG_OWN_PAGES (1U << 1)
20
21/*
22 * crypto operations for filesystems
23 */
24struct fscrypt_operations {
25 unsigned int flags;
26 const char *key_prefix;
27 int (*get_context)(struct inode *, void *, size_t);
28 int (*set_context)(struct inode *, const void *, size_t, void *);
29 bool (*dummy_context)(struct inode *);
30 bool (*empty_dir)(struct inode *);
Eric Biggers4ddc3a82018-04-30 15:51:44 -070031 unsigned int max_namelen;
Eric Biggersefbfa8c2018-01-05 10:44:57 -080032};
33
Eric Biggersf0c472b2018-01-05 10:44:55 -080034struct fscrypt_ctx {
35 union {
36 struct {
37 struct page *bounce_page; /* Ciphertext page */
38 struct page *control_page; /* Original page */
39 } w;
40 struct {
41 struct bio *bio;
42 struct work_struct work;
43 } r;
44 struct list_head free_list; /* Free list */
45 };
46 u8 flags; /* Flags */
47};
48
Eric Biggersd8dfb892018-01-05 10:44:52 -080049static inline bool fscrypt_has_encryption_key(const struct inode *inode)
50{
51 return (inode->i_crypt_info != NULL);
52}
53
Eric Biggers616dbd22018-01-05 10:44:56 -080054static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
55{
56 return inode->i_sb->s_cop->dummy_context &&
57 inode->i_sb->s_cop->dummy_context(inode);
58}
59
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070060/* crypto.c */
Eric Biggers05099232018-04-18 11:09:47 -070061extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070062extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
63extern void fscrypt_release_ctx(struct fscrypt_ctx *);
64extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
65 unsigned int, unsigned int,
66 u64, gfp_t);
67extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
68 unsigned int, u64);
Eric Biggersb67b07e2018-01-05 10:44:53 -080069
70static inline struct page *fscrypt_control_page(struct page *page)
71{
72 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
73}
74
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070075extern void fscrypt_restore_control_page(struct page *);
76
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070077/* policy.c */
78extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
79extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
80extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
81extern int fscrypt_inherit_context(struct inode *, struct inode *,
82 void *, bool);
83/* keyinfo.c */
84extern int fscrypt_get_encryption_info(struct inode *);
Eric Biggers401052f2018-01-11 23:30:13 -050085extern void fscrypt_put_encryption_info(struct inode *);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070086
87/* fname.c */
88extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
89 int lookup, struct fscrypt_name *);
Eric Biggers05b79cd2017-05-22 18:14:06 -070090
91static inline void fscrypt_free_filename(struct fscrypt_name *fname)
92{
93 kfree(fname->crypto_buf.name);
94}
95
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070096extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
97 struct fscrypt_str *);
98extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
99extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
100 const struct fscrypt_str *, struct fscrypt_str *);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700101
102#define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE 32
103
104/* Extracts the second-to-last ciphertext block; see explanation below */
105#define FSCRYPT_FNAME_DIGEST(name, len) \
106 ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
107 FS_CRYPTO_BLOCK_SIZE))
108
109#define FSCRYPT_FNAME_DIGEST_SIZE FS_CRYPTO_BLOCK_SIZE
110
111/**
112 * fscrypt_digested_name - alternate identifier for an on-disk filename
113 *
114 * When userspace lists an encrypted directory without access to the key,
115 * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
116 * bytes are shown in this abbreviated form (base64-encoded) rather than as the
117 * full ciphertext (base64-encoded). This is necessary to allow supporting
118 * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
119 *
120 * To make it possible for filesystems to still find the correct directory entry
121 * despite not knowing the full on-disk name, we encode any filesystem-specific
122 * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
123 * followed by the second-to-last ciphertext block of the filename. Due to the
124 * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
125 * depends on the full plaintext. (Note that ciphertext stealing causes the
126 * last two blocks to appear "flipped".) This makes accidental collisions very
127 * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
128 * share the same filesystem-specific hashes.
129 *
130 * However, this scheme isn't immune to intentional collisions, which can be
131 * created by anyone able to create arbitrary plaintext filenames and view them
132 * without the key. Making the "digest" be a real cryptographic hash like
133 * SHA-256 over the full ciphertext would prevent this, although it would be
134 * less efficient and harder to implement, especially since the filesystem would
135 * need to calculate it for each directory entry examined during a search.
136 */
137struct fscrypt_digested_name {
138 u32 hash;
139 u32 minor_hash;
140 u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
141};
142
143/**
144 * fscrypt_match_name() - test whether the given name matches a directory entry
145 * @fname: the name being searched for
146 * @de_name: the name from the directory entry
147 * @de_name_len: the length of @de_name in bytes
148 *
149 * Normally @fname->disk_name will be set, and in that case we simply compare
150 * that to the name stored in the directory entry. The only exception is that
151 * if we don't have the key for an encrypted directory and a filename in it is
152 * very long, then we won't have the full disk_name and we'll instead need to
153 * match against the fscrypt_digested_name.
154 *
155 * Return: %true if the name matches, otherwise %false.
156 */
157static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
158 const u8 *de_name, u32 de_name_len)
159{
160 if (unlikely(!fname->disk_name.name)) {
161 const struct fscrypt_digested_name *n =
162 (const void *)fname->crypto_buf.name;
163 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
164 return false;
165 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
166 return false;
167 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
168 n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
169 }
170
171 if (de_name_len != fname->disk_name.len)
172 return false;
173 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
174}
175
176/* bio.c */
Eric Biggers05099232018-04-18 11:09:47 -0700177extern void fscrypt_decrypt_bio(struct bio *);
178extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
179 struct bio *bio);
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700180extern void fscrypt_pullback_bio_page(struct page **, bool);
181extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
182 unsigned int);
183
Eric Biggersd61dffb2017-10-09 12:15:40 -0700184/* hooks.c */
185extern int fscrypt_file_open(struct inode *inode, struct file *filp);
Eric Biggersf5218702017-10-09 12:15:41 -0700186extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir);
Eric Biggers98fe83a2017-10-09 12:15:42 -0700187extern int __fscrypt_prepare_rename(struct inode *old_dir,
188 struct dentry *old_dentry,
189 struct inode *new_dir,
190 struct dentry *new_dentry,
191 unsigned int flags);
Eric Biggersec822ff2017-10-09 12:15:43 -0700192extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry);
Eric Biggersa7e05c72018-01-05 10:45:01 -0800193extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
194 unsigned int max_len,
195 struct fscrypt_str *disk_link);
196extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
197 unsigned int len,
198 struct fscrypt_str *disk_link);
Eric Biggersda32a162018-01-05 10:45:02 -0800199extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
200 unsigned int max_size,
201 struct delayed_call *done);
Eric Biggersd61dffb2017-10-09 12:15:40 -0700202
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700203#endif /* _LINUX_FSCRYPT_SUPP_H */