Skip to content

Commit f0d8744

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt
Pull fscrypt updates from Eric Biggers: - Extend the FS_IOC_ADD_ENCRYPTION_KEY ioctl to allow the raw key to be provided via a keyring key. - Prepare for the new dirhash method (SipHash of plaintext name) that will be used by directories that are both encrypted and casefolded. - Switch to a new format for "no-key names" that prepares for the new dirhash method, and also fixes a longstanding bug where multiple filenames could map to the same no-key name. - Allow the crypto algorithms used by fscrypt to be built as loadable modules when the fscrypt-capable filesystems are. - Optimize fscrypt_zeroout_range(). - Various cleanups. * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/fscrypt: (26 commits) fscrypt: improve format of no-key names ubifs: allow both hash and disk name to be provided in no-key names ubifs: don't trigger assertion on invalid no-key filename fscrypt: clarify what is meant by a per-file key fscrypt: derive dirhash key for casefolded directories fscrypt: don't allow v1 policies with casefolding fscrypt: add "fscrypt_" prefix to fname_encrypt() fscrypt: don't print name of busy file when removing key ubifs: use IS_ENCRYPTED() instead of ubifs_crypt_is_encrypted() fscrypt: document gfp_flags for bounce page allocation fscrypt: optimize fscrypt_zeroout_range() fscrypt: remove redundant bi_status check fscrypt: Allow modular crypto algorithms fscrypt: include <linux/ioctl.h> in UAPI header fscrypt: don't check for ENOKEY from fscrypt_get_encryption_info() fscrypt: remove fscrypt_is_direct_key_policy() fscrypt: move fscrypt_valid_enc_modes() to policy.c fscrypt: check for appropriate use of DIRECT_KEY flag earlier fscrypt: split up fscrypt_supported_policy() by policy version fscrypt: introduce fscrypt_needs_contents_encryption() ...
2 parents b5f7ab6 + edc440e commit f0d8744

File tree

25 files changed

+864
-449
lines changed

25 files changed

+864
-449
lines changed

Documentation/filesystems/fscrypt.rst

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ HKDF is more flexible, is nonreversible, and evenly distributes
234234
entropy from the master key. HKDF is also standardized and widely
235235
used by other software, whereas the AES-128-ECB based KDF is ad-hoc.
236236

237-
Per-file keys
238-
-------------
237+
Per-file encryption keys
238+
------------------------
239239

240240
Since each master key can protect many files, it is necessary to
241241
"tweak" the encryption of each file so that the same plaintext in two
@@ -268,9 +268,9 @@ is greater than that of an AES-256-XTS key.
268268
Therefore, to improve performance and save memory, for Adiantum a
269269
"direct key" configuration is supported. When the user has enabled
270270
this by setting FSCRYPT_POLICY_FLAG_DIRECT_KEY in the fscrypt policy,
271-
per-file keys are not used. Instead, whenever any data (contents or
272-
filenames) is encrypted, the file's 16-byte nonce is included in the
273-
IV. Moreover:
271+
per-file encryption keys are not used. Instead, whenever any data
272+
(contents or filenames) is encrypted, the file's 16-byte nonce is
273+
included in the IV. Moreover:
274274

275275
- For v1 encryption policies, the encryption is done directly with the
276276
master key. Because of this, users **must not** use the same master
@@ -302,6 +302,16 @@ For master keys used for v2 encryption policies, a unique 16-byte "key
302302
identifier" is also derived using the KDF. This value is stored in
303303
the clear, since it is needed to reliably identify the key itself.
304304

305+
Dirhash keys
306+
------------
307+
308+
For directories that are indexed using a secret-keyed dirhash over the
309+
plaintext filenames, the KDF is also used to derive a 128-bit
310+
SipHash-2-4 key per directory in order to hash filenames. This works
311+
just like deriving a per-file encryption key, except that a different
312+
KDF context is used. Currently, only casefolded ("case-insensitive")
313+
encrypted directories use this style of hashing.
314+
305315
Encryption modes and usage
306316
==========================
307317

@@ -325,11 +335,11 @@ used.
325335
Adiantum is a (primarily) stream cipher-based mode that is fast even
326336
on CPUs without dedicated crypto instructions. It's also a true
327337
wide-block mode, unlike XTS. It can also eliminate the need to derive
328-
per-file keys. However, it depends on the security of two primitives,
329-
XChaCha12 and AES-256, rather than just one. See the paper
330-
"Adiantum: length-preserving encryption for entry-level processors"
331-
(https://eprint.iacr.org/2018/720.pdf) for more details. To use
332-
Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
338+
per-file encryption keys. However, it depends on the security of two
339+
primitives, XChaCha12 and AES-256, rather than just one. See the
340+
paper "Adiantum: length-preserving encryption for entry-level
341+
processors" (https://eprint.iacr.org/2018/720.pdf) for more details.
342+
To use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
333343
implementations of ChaCha and NHPoly1305 should be enabled, e.g.
334344
CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM.
335345

@@ -513,7 +523,9 @@ FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors:
513523
- ``EEXIST``: the file is already encrypted with an encryption policy
514524
different from the one specified
515525
- ``EINVAL``: an invalid encryption policy was specified (invalid
516-
version, mode(s), or flags; or reserved bits were set)
526+
version, mode(s), or flags; or reserved bits were set); or a v1
527+
encryption policy was specified but the directory has the casefold
528+
flag enabled (casefolding is incompatible with v1 policies).
517529
- ``ENOKEY``: a v2 encryption policy was specified, but the key with
518530
the specified ``master_key_identifier`` has not been added, nor does
519531
the process have the CAP_FOWNER capability in the initial user
@@ -638,7 +650,8 @@ follows::
638650
struct fscrypt_add_key_arg {
639651
struct fscrypt_key_specifier key_spec;
640652
__u32 raw_size;
641-
__u32 __reserved[9];
653+
__u32 key_id;
654+
__u32 __reserved[8];
642655
__u8 raw[];
643656
};
644657

@@ -655,6 +668,12 @@ follows::
655668
} u;
656669
};
657670

671+
struct fscrypt_provisioning_key_payload {
672+
__u32 type;
673+
__u32 __reserved;
674+
__u8 raw[];
675+
};
676+
658677
:c:type:`struct fscrypt_add_key_arg` must be zeroed, then initialized
659678
as follows:
660679

@@ -677,9 +696,26 @@ as follows:
677696
``Documentation/security/keys/core.rst``).
678697

679698
- ``raw_size`` must be the size of the ``raw`` key provided, in bytes.
699+
Alternatively, if ``key_id`` is nonzero, this field must be 0, since
700+
in that case the size is implied by the specified Linux keyring key.
701+
702+
- ``key_id`` is 0 if the raw key is given directly in the ``raw``
703+
field. Otherwise ``key_id`` is the ID of a Linux keyring key of
704+
type "fscrypt-provisioning" whose payload is a :c:type:`struct
705+
fscrypt_provisioning_key_payload` whose ``raw`` field contains the
706+
raw key and whose ``type`` field matches ``key_spec.type``. Since
707+
``raw`` is variable-length, the total size of this key's payload
708+
must be ``sizeof(struct fscrypt_provisioning_key_payload)`` plus the
709+
raw key size. The process must have Search permission on this key.
710+
711+
Most users should leave this 0 and specify the raw key directly.
712+
The support for specifying a Linux keyring key is intended mainly to
713+
allow re-adding keys after a filesystem is unmounted and re-mounted,
714+
without having to store the raw keys in userspace memory.
680715

681716
- ``raw`` is a variable-length field which must contain the actual
682-
key, ``raw_size`` bytes long.
717+
key, ``raw_size`` bytes long. Alternatively, if ``key_id`` is
718+
nonzero, then this field is unused.
683719

684720
For v2 policy keys, the kernel keeps track of which user (identified
685721
by effective user ID) added the key, and only allows the key to be
@@ -701,11 +737,16 @@ FS_IOC_ADD_ENCRYPTION_KEY can fail with the following errors:
701737

702738
- ``EACCES``: FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR was specified, but the
703739
caller does not have the CAP_SYS_ADMIN capability in the initial
704-
user namespace
740+
user namespace; or the raw key was specified by Linux key ID but the
741+
process lacks Search permission on the key.
705742
- ``EDQUOT``: the key quota for this user would be exceeded by adding
706743
the key
707744
- ``EINVAL``: invalid key size or key specifier type, or reserved bits
708745
were set
746+
- ``EKEYREJECTED``: the raw key was specified by Linux key ID, but the
747+
key has the wrong type
748+
- ``ENOKEY``: the raw key was specified by Linux key ID, but no key
749+
exists with that ID
709750
- ``ENOTTY``: this type of filesystem does not implement encryption
710751
- ``EOPNOTSUPP``: the kernel was not configured with encryption
711752
support for this filesystem, or the filesystem superblock has not
@@ -1108,8 +1149,8 @@ The context structs contain the same information as the corresponding
11081149
policy structs (see `Setting an encryption policy`_), except that the
11091150
context structs also contain a nonce. The nonce is randomly generated
11101151
by the kernel and is used as KDF input or as a tweak to cause
1111-
different files to be encrypted differently; see `Per-file keys`_ and
1112-
`DIRECT_KEY policies`_.
1152+
different files to be encrypted differently; see `Per-file encryption
1153+
keys`_ and `DIRECT_KEY policies`_.
11131154

11141155
Data path changes
11151156
-----------------
@@ -1161,7 +1202,7 @@ filesystem-specific hash(es) needed for directory lookups. This
11611202
allows the filesystem to still, with a high degree of confidence, map
11621203
the filename given in ->lookup() back to a particular directory entry
11631204
that was previously listed by readdir(). See :c:type:`struct
1164-
fscrypt_digested_name` in the source for more details.
1205+
fscrypt_nokey_name` in the source for more details.
11651206

11661207
Note that the precise way that filenames are presented to userspace
11671208
without the key is subject to change in the future. It is only meant

fs/crypto/Kconfig

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@
22
config FS_ENCRYPTION
33
bool "FS Encryption (Per-file encryption)"
44
select CRYPTO
5-
select CRYPTO_AES
6-
select CRYPTO_CBC
7-
select CRYPTO_ECB
8-
select CRYPTO_XTS
9-
select CRYPTO_CTS
10-
select CRYPTO_SHA512
11-
select CRYPTO_HMAC
5+
select CRYPTO_HASH
6+
select CRYPTO_SKCIPHER
127
select KEYS
138
help
149
Enable encryption of files and directories. This
1510
feature is similar to ecryptfs, but it is more memory
1611
efficient since it avoids caching the encrypted and
1712
decrypted pages in the page cache. Currently Ext4,
1813
F2FS and UBIFS make use of this feature.
14+
15+
# Filesystems supporting encryption must select this if FS_ENCRYPTION. This
16+
# allows the algorithms to be built as modules when all the filesystems are.
17+
config FS_ENCRYPTION_ALGS
18+
tristate
19+
select CRYPTO_AES
20+
select CRYPTO_CBC
21+
select CRYPTO_CTS
22+
select CRYPTO_ECB
23+
select CRYPTO_HMAC
24+
select CRYPTO_SHA256
25+
select CRYPTO_SHA512
26+
select CRYPTO_XTS

fs/crypto/bio.c

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,53 +41,101 @@ void fscrypt_decrypt_bio(struct bio *bio)
4141
}
4242
EXPORT_SYMBOL(fscrypt_decrypt_bio);
4343

44+
/**
45+
* fscrypt_zeroout_range() - zero out a range of blocks in an encrypted file
46+
* @inode: the file's inode
47+
* @lblk: the first file logical block to zero out
48+
* @pblk: the first filesystem physical block to zero out
49+
* @len: number of blocks to zero out
50+
*
51+
* Zero out filesystem blocks in an encrypted regular file on-disk, i.e. write
52+
* ciphertext blocks which decrypt to the all-zeroes block. The blocks must be
53+
* both logically and physically contiguous. It's also assumed that the
54+
* filesystem only uses a single block device, ->s_bdev.
55+
*
56+
* Note that since each block uses a different IV, this involves writing a
57+
* different ciphertext to each block; we can't simply reuse the same one.
58+
*
59+
* Return: 0 on success; -errno on failure.
60+
*/
4461
int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
45-
sector_t pblk, unsigned int len)
62+
sector_t pblk, unsigned int len)
4663
{
4764
const unsigned int blockbits = inode->i_blkbits;
4865
const unsigned int blocksize = 1 << blockbits;
49-
struct page *ciphertext_page;
66+
const unsigned int blocks_per_page_bits = PAGE_SHIFT - blockbits;
67+
const unsigned int blocks_per_page = 1 << blocks_per_page_bits;
68+
struct page *pages[16]; /* write up to 16 pages at a time */
69+
unsigned int nr_pages;
70+
unsigned int i;
71+
unsigned int offset;
5072
struct bio *bio;
51-
int ret, err = 0;
73+
int ret, err;
5274

53-
ciphertext_page = fscrypt_alloc_bounce_page(GFP_NOWAIT);
54-
if (!ciphertext_page)
55-
return -ENOMEM;
75+
if (len == 0)
76+
return 0;
5677

57-
while (len--) {
58-
err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
59-
ZERO_PAGE(0), ciphertext_page,
60-
blocksize, 0, GFP_NOFS);
61-
if (err)
62-
goto errout;
78+
BUILD_BUG_ON(ARRAY_SIZE(pages) > BIO_MAX_PAGES);
79+
nr_pages = min_t(unsigned int, ARRAY_SIZE(pages),
80+
(len + blocks_per_page - 1) >> blocks_per_page_bits);
6381

64-
bio = bio_alloc(GFP_NOWAIT, 1);
65-
if (!bio) {
66-
err = -ENOMEM;
67-
goto errout;
68-
}
82+
/*
83+
* We need at least one page for ciphertext. Allocate the first one
84+
* from a mempool, with __GFP_DIRECT_RECLAIM set so that it can't fail.
85+
*
86+
* Any additional page allocations are allowed to fail, as they only
87+
* help performance, and waiting on the mempool for them could deadlock.
88+
*/
89+
for (i = 0; i < nr_pages; i++) {
90+
pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
91+
GFP_NOWAIT | __GFP_NOWARN);
92+
if (!pages[i])
93+
break;
94+
}
95+
nr_pages = i;
96+
if (WARN_ON(nr_pages <= 0))
97+
return -EINVAL;
98+
99+
/* This always succeeds since __GFP_DIRECT_RECLAIM is set. */
100+
bio = bio_alloc(GFP_NOFS, nr_pages);
101+
102+
do {
69103
bio_set_dev(bio, inode->i_sb->s_bdev);
70104
bio->bi_iter.bi_sector = pblk << (blockbits - 9);
71105
bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
72-
ret = bio_add_page(bio, ciphertext_page, blocksize, 0);
73-
if (WARN_ON(ret != blocksize)) {
74-
/* should never happen! */
75-
bio_put(bio);
76-
err = -EIO;
77-
goto errout;
78-
}
106+
107+
i = 0;
108+
offset = 0;
109+
do {
110+
err = fscrypt_crypt_block(inode, FS_ENCRYPT, lblk,
111+
ZERO_PAGE(0), pages[i],
112+
blocksize, offset, GFP_NOFS);
113+
if (err)
114+
goto out;
115+
lblk++;
116+
pblk++;
117+
len--;
118+
offset += blocksize;
119+
if (offset == PAGE_SIZE || len == 0) {
120+
ret = bio_add_page(bio, pages[i++], offset, 0);
121+
if (WARN_ON(ret != offset)) {
122+
err = -EIO;
123+
goto out;
124+
}
125+
offset = 0;
126+
}
127+
} while (i != nr_pages && len != 0);
128+
79129
err = submit_bio_wait(bio);
80-
if (err == 0 && bio->bi_status)
81-
err = -EIO;
82-
bio_put(bio);
83130
if (err)
84-
goto errout;
85-
lblk++;
86-
pblk++;
87-
}
131+
goto out;
132+
bio_reset(bio);
133+
} while (len != 0);
88134
err = 0;
89-
errout:
90-
fscrypt_free_bounce_page(ciphertext_page);
135+
out:
136+
bio_put(bio);
137+
for (i = 0; i < nr_pages; i++)
138+
fscrypt_free_bounce_page(pages[i]);
91139
return err;
92140
}
93141
EXPORT_SYMBOL(fscrypt_zeroout_range);

0 commit comments

Comments
 (0)