diff options
| -rw-r--r-- | Documentation/filesystems/fscrypt.rst | 610 | ||||
| -rw-r--r-- | Documentation/filesystems/index.rst | 11 | ||||
| -rw-r--r-- | MAINTAINERS | 1 | ||||
| -rw-r--r-- | fs/crypto/Makefile | 2 | ||||
| -rw-r--r-- | fs/crypto/crypto.c | 9 | ||||
| -rw-r--r-- | fs/crypto/fname.c | 3 | ||||
| -rw-r--r-- | fs/crypto/fscrypt_private.h | 3 | ||||
| -rw-r--r-- | fs/crypto/hooks.c | 112 | ||||
| -rw-r--r-- | fs/crypto/keyinfo.c | 2 | ||||
| -rw-r--r-- | fs/crypto/policy.c | 6 | ||||
| -rw-r--r-- | fs/ext4/ext4.h | 8 | ||||
| -rw-r--r-- | fs/ext4/file.c | 23 | ||||
| -rw-r--r-- | fs/ext4/inode.c | 19 | ||||
| -rw-r--r-- | fs/ext4/namei.c | 62 | ||||
| -rw-r--r-- | fs/ext4/super.c | 15 | ||||
| -rw-r--r-- | fs/f2fs/f2fs.h | 9 | ||||
| -rw-r--r-- | fs/f2fs/inode.c | 5 | ||||
| -rw-r--r-- | fs/f2fs/super.c | 7 | ||||
| -rw-r--r-- | fs/ubifs/crypto.c | 1 | ||||
| -rw-r--r-- | fs/ubifs/ioctl.c | 5 | ||||
| -rw-r--r-- | fs/ubifs/super.c | 8 | ||||
| -rw-r--r-- | fs/ubifs/ubifs.h | 18 | ||||
| -rw-r--r-- | fs/ubifs/xattr.c | 1 | ||||
| -rw-r--r-- | include/linux/fs.h | 2 | ||||
| -rw-r--r-- | include/linux/fscrypt.h | 294 | ||||
| -rw-r--r-- | include/linux/fscrypt_common.h | 142 | ||||
| -rw-r--r-- | include/linux/fscrypt_notsupp.h | 39 | ||||
| -rw-r--r-- | include/linux/fscrypt_supp.h | 17 |
28 files changed, 1155 insertions, 279 deletions
diff --git a/Documentation/filesystems/fscrypt.rst b/Documentation/filesystems/fscrypt.rst new file mode 100644 index 000000000000..776ddc655f79 --- /dev/null +++ b/Documentation/filesystems/fscrypt.rst | |||
| @@ -0,0 +1,610 @@ | |||
| 1 | ===================================== | ||
| 2 | Filesystem-level encryption (fscrypt) | ||
| 3 | ===================================== | ||
| 4 | |||
| 5 | Introduction | ||
| 6 | ============ | ||
| 7 | |||
| 8 | fscrypt is a library which filesystems can hook into to support | ||
| 9 | transparent encryption of files and directories. | ||
| 10 | |||
| 11 | Note: "fscrypt" in this document refers to the kernel-level portion, | ||
| 12 | implemented in ``fs/crypto/``, as opposed to the userspace tool | ||
| 13 | `fscrypt <https://github.com/google/fscrypt>`_. This document only | ||
| 14 | covers the kernel-level portion. For command-line examples of how to | ||
| 15 | use encryption, see the documentation for the userspace tool `fscrypt | ||
| 16 | <https://github.com/google/fscrypt>`_. Also, it is recommended to use | ||
| 17 | the fscrypt userspace tool, or other existing userspace tools such as | ||
| 18 | `fscryptctl <https://github.com/google/fscryptctl>`_ or `Android's key | ||
| 19 | management system | ||
| 20 | <https://source.android.com/security/encryption/file-based>`_, over | ||
| 21 | using the kernel's API directly. Using existing tools reduces the | ||
| 22 | chance of introducing your own security bugs. (Nevertheless, for | ||
| 23 | completeness this documentation covers the kernel's API anyway.) | ||
| 24 | |||
| 25 | Unlike dm-crypt, fscrypt operates at the filesystem level rather than | ||
| 26 | at the block device level. This allows it to encrypt different files | ||
| 27 | with different keys and to have unencrypted files on the same | ||
| 28 | filesystem. This is useful for multi-user systems where each user's | ||
| 29 | data-at-rest needs to be cryptographically isolated from the others. | ||
| 30 | However, except for filenames, fscrypt does not encrypt filesystem | ||
| 31 | metadata. | ||
| 32 | |||
| 33 | Unlike eCryptfs, which is a stacked filesystem, fscrypt is integrated | ||
| 34 | directly into supported filesystems --- currently ext4, F2FS, and | ||
| 35 | UBIFS. This allows encrypted files to be read and written without | ||
| 36 | caching both the decrypted and encrypted pages in the pagecache, | ||
| 37 | thereby nearly halving the memory used and bringing it in line with | ||
| 38 | unencrypted files. Similarly, half as many dentries and inodes are | ||
| 39 | needed. eCryptfs also limits encrypted filenames to 143 bytes, | ||
| 40 | causing application compatibility issues; fscrypt allows the full 255 | ||
| 41 | bytes (NAME_MAX). Finally, unlike eCryptfs, the fscrypt API can be | ||
| 42 | used by unprivileged users, with no need to mount anything. | ||
| 43 | |||
| 44 | fscrypt does not support encrypting files in-place. Instead, it | ||
| 45 | supports marking an empty directory as encrypted. Then, after | ||
| 46 | userspace provides the key, all regular files, directories, and | ||
| 47 | symbolic links created in that directory tree are transparently | ||
| 48 | encrypted. | ||
| 49 | |||
| 50 | Threat model | ||
| 51 | ============ | ||
| 52 | |||
| 53 | Offline attacks | ||
| 54 | --------------- | ||
| 55 | |||
| 56 | Provided that userspace chooses a strong encryption key, fscrypt | ||
| 57 | protects the confidentiality of file contents and filenames in the | ||
| 58 | event of a single point-in-time permanent offline compromise of the | ||
| 59 | block device content. fscrypt does not protect the confidentiality of | ||
| 60 | non-filename metadata, e.g. file sizes, file permissions, file | ||
| 61 | timestamps, and extended attributes. Also, the existence and location | ||
| 62 | of holes (unallocated blocks which logically contain all zeroes) in | ||
| 63 | files is not protected. | ||
| 64 | |||
| 65 | fscrypt is not guaranteed to protect confidentiality or authenticity | ||
| 66 | if an attacker is able to manipulate the filesystem offline prior to | ||
| 67 | an authorized user later accessing the filesystem. | ||
| 68 | |||
| 69 | Online attacks | ||
| 70 | -------------- | ||
| 71 | |||
| 72 | fscrypt (and storage encryption in general) can only provide limited | ||
| 73 | protection, if any at all, against online attacks. In detail: | ||
| 74 | |||
| 75 | fscrypt is only resistant to side-channel attacks, such as timing or | ||
| 76 | electromagnetic attacks, to the extent that the underlying Linux | ||
| 77 | Cryptographic API algorithms are. If a vulnerable algorithm is used, | ||
| 78 | such as a table-based implementation of AES, it may be possible for an | ||
| 79 | attacker to mount a side channel attack against the online system. | ||
| 80 | Side channel attacks may also be mounted against applications | ||
| 81 | consuming decrypted data. | ||
| 82 | |||
| 83 | After an encryption key has been provided, fscrypt is not designed to | ||
| 84 | hide the plaintext file contents or filenames from other users on the | ||
| 85 | same system, regardless of the visibility of the keyring key. | ||
| 86 | Instead, existing access control mechanisms such as file mode bits, | ||
| 87 | POSIX ACLs, LSMs, or mount namespaces should be used for this purpose. | ||
| 88 | Also note that as long as the encryption keys are *anywhere* in | ||
| 89 | memory, an online attacker can necessarily compromise them by mounting | ||
| 90 | a physical attack or by exploiting any kernel security vulnerability | ||
| 91 | which provides an arbitrary memory read primitive. | ||
| 92 | |||
| 93 | While it is ostensibly possible to "evict" keys from the system, | ||
| 94 | recently accessed encrypted files will remain accessible at least | ||
| 95 | until the filesystem is unmounted or the VFS caches are dropped, e.g. | ||
| 96 | using ``echo 2 > /proc/sys/vm/drop_caches``. Even after that, if the | ||
| 97 | RAM is compromised before being powered off, it will likely still be | ||
| 98 | possible to recover portions of the plaintext file contents, if not | ||
| 99 | some of the encryption keys as well. (Since Linux v4.12, all | ||
| 100 | in-kernel keys related to fscrypt are sanitized before being freed. | ||
| 101 | However, userspace would need to do its part as well.) | ||
| 102 | |||
| 103 | Currently, fscrypt does not prevent a user from maliciously providing | ||
| 104 | an incorrect key for another user's existing encrypted files. A | ||
| 105 | protection against this is planned. | ||
| 106 | |||
| 107 | Key hierarchy | ||
| 108 | ============= | ||
| 109 | |||
| 110 | Master Keys | ||
| 111 | ----------- | ||
| 112 | |||
| 113 | Each encrypted directory tree is protected by a *master key*. Master | ||
| 114 | keys can be up to 64 bytes long, and must be at least as long as the | ||
| 115 | greater of the key length needed by the contents and filenames | ||
| 116 | encryption modes being used. For example, if AES-256-XTS is used for | ||
| 117 | contents encryption, the master key must be 64 bytes (512 bits). Note | ||
| 118 | that the XTS mode is defined to require a key twice as long as that | ||
| 119 | required by the underlying block cipher. | ||
| 120 | |||
| 121 | To "unlock" an encrypted directory tree, userspace must provide the | ||
| 122 | appropriate master key. There can be any number of master keys, each | ||
| 123 | of which protects any number of directory trees on any number of | ||
| 124 | filesystems. | ||
| 125 | |||
| 126 | Userspace should generate master keys either using a cryptographically | ||
| 127 | secure random number generator, or by using a KDF (Key Derivation | ||
| 128 | Function). Note that whenever a KDF is used to "stretch" a | ||
| 129 | lower-entropy secret such as a passphrase, it is critical that a KDF | ||
| 130 | designed for this purpose be used, such as scrypt, PBKDF2, or Argon2. | ||
| 131 | |||
| 132 | Per-file keys | ||
| 133 | ------------- | ||
| 134 | |||
| 135 | Master keys are not used to encrypt file contents or names directly. | ||
| 136 | Instead, a unique key is derived for each encrypted file, including | ||
| 137 | each regular file, directory, and symbolic link. This has several | ||
| 138 | advantages: | ||
| 139 | |||
| 140 | - In cryptosystems, the same key material should never be used for | ||
| 141 | different purposes. Using the master key as both an XTS key for | ||
| 142 | contents encryption and as a CTS-CBC key for filenames encryption | ||
| 143 | would violate this rule. | ||
| 144 | - Per-file keys simplify the choice of IVs (Initialization Vectors) | ||
| 145 | for contents encryption. Without per-file keys, to ensure IV | ||
| 146 | uniqueness both the inode and logical block number would need to be | ||
| 147 | encoded in the IVs. This would make it impossible to renumber | ||
| 148 | inodes, which e.g. ``resize2fs`` can do when resizing an ext4 | ||
| 149 | filesystem. With per-file keys, it is sufficient to encode just the | ||
| 150 | logical block number in the IVs. | ||
| 151 | - Per-file keys strengthen the encryption of filenames, where IVs are | ||
| 152 | reused out of necessity. With a unique key per directory, IV reuse | ||
| 153 | is limited to within a single directory. | ||
| 154 | - Per-file keys allow individual files to be securely erased simply by | ||
| 155 | securely erasing their keys. (Not yet implemented.) | ||
| 156 | |||
| 157 | A KDF (Key Derivation Function) is used to derive per-file keys from | ||
| 158 | the master key. This is done instead of wrapping a randomly-generated | ||
| 159 | key for each file because it reduces the size of the encryption xattr, | ||
| 160 | which for some filesystems makes the xattr more likely to fit in-line | ||
| 161 | in the filesystem's inode table. With a KDF, only a 16-byte nonce is | ||
| 162 | required --- long enough to make key reuse extremely unlikely. A | ||
| 163 | wrapped key, on the other hand, would need to be up to 64 bytes --- | ||
| 164 | the length of an AES-256-XTS key. Furthermore, currently there is no | ||
| 165 | requirement to support unlocking a file with multiple alternative | ||
| 166 | master keys or to support rotating master keys. Instead, the master | ||
| 167 | keys may be wrapped in userspace, e.g. as done by the `fscrypt | ||
| 168 | <https://github.com/google/fscrypt>`_ tool. | ||
| 169 | |||
| 170 | The current KDF encrypts the master key using the 16-byte nonce as an | ||
| 171 | AES-128-ECB key. The output is used as the derived key. If the | ||
| 172 | output is longer than needed, then it is truncated to the needed | ||
| 173 | length. Truncation is the norm for directories and symlinks, since | ||
| 174 | those use the CTS-CBC encryption mode which requires a key half as | ||
| 175 | long as that required by the XTS encryption mode. | ||
| 176 | |||
| 177 | Note: this KDF meets the primary security requirement, which is to | ||
| 178 | produce unique derived keys that preserve the entropy of the master | ||
| 179 | key, assuming that the master key is already a good pseudorandom key. | ||
| 180 | However, it is nonstandard and has some problems such as being | ||
| 181 | reversible, so it is generally considered to be a mistake! It may be | ||
| 182 | replaced with HKDF or another more standard KDF in the future. | ||
| 183 | |||
| 184 | Encryption modes and usage | ||
| 185 | ========================== | ||
| 186 | |||
| 187 | fscrypt allows one encryption mode to be specified for file contents | ||
| 188 | and one encryption mode to be specified for filenames. Different | ||
| 189 | directory trees are permitted to use different encryption modes. | ||
| 190 | Currently, the following pairs of encryption modes are supported: | ||
| 191 | |||
| 192 | - AES-256-XTS for contents and AES-256-CTS-CBC for filenames | ||
| 193 | - AES-128-CBC for contents and AES-128-CTS-CBC for filenames | ||
| 194 | |||
| 195 | It is strongly recommended to use AES-256-XTS for contents encryption. | ||
| 196 | AES-128-CBC was added only for low-powered embedded devices with | ||
| 197 | crypto accelerators such as CAAM or CESA that do not support XTS. | ||
| 198 | |||
| 199 | New encryption modes can be added relatively easily, without changes | ||
| 200 | to individual filesystems. However, authenticated encryption (AE) | ||
| 201 | modes are not currently supported because of the difficulty of dealing | ||
| 202 | with ciphertext expansion. | ||
| 203 | |||
| 204 | For file contents, each filesystem block is encrypted independently. | ||
| 205 | Currently, only the case where the filesystem block size is equal to | ||
| 206 | the system's page size (usually 4096 bytes) is supported. With the | ||
| 207 | XTS mode of operation (recommended), the logical block number within | ||
| 208 | the file is used as the IV. With the CBC mode of operation (not | ||
| 209 | recommended), ESSIV is used; specifically, the IV for CBC is the | ||
| 210 | logical block number encrypted with AES-256, where the AES-256 key is | ||
| 211 | the SHA-256 hash of the inode's data encryption key. | ||
| 212 | |||
| 213 | For filenames, the full filename is encrypted at once. Because of the | ||
| 214 | requirements to retain support for efficient directory lookups and | ||
| 215 | filenames of up to 255 bytes, a constant initialization vector (IV) is | ||
| 216 | used. However, each encrypted directory uses a unique key, which | ||
| 217 | limits IV reuse to within a single directory. Note that IV reuse in | ||
| 218 | the context of CTS-CBC encryption means that when the original | ||
| 219 | filenames share a common prefix at least as long as the cipher block | ||
| 220 | size (16 bytes for AES), the corresponding encrypted filenames will | ||
| 221 | also share a common prefix. This is undesirable; it may be fixed in | ||
| 222 | the future by switching to an encryption mode that is a strong | ||
| 223 | pseudorandom permutation on arbitrary-length messages, e.g. the HEH | ||
| 224 | (Hash-Encrypt-Hash) mode. | ||
| 225 | |||
| 226 | Since filenames are encrypted with the CTS-CBC mode of operation, the | ||
| 227 | plaintext and ciphertext filenames need not be multiples of the AES | ||
| 228 | block size, i.e. 16 bytes. However, the minimum size that can be | ||
| 229 | encrypted is 16 bytes, so shorter filenames are NUL-padded to 16 bytes | ||
| 230 | before being encrypted. In addition, to reduce leakage of filename | ||
| 231 | lengths via their ciphertexts, all filenames are NUL-padded to the | ||
| 232 | next 4, 8, 16, or 32-byte boundary (configurable). 32 is recommended | ||
| 233 | since this provides the best confidentiality, at the cost of making | ||
| 234 | directory entries consume slightly more space. Note that since NUL | ||
| 235 | (``\0``) is not otherwise a valid character in filenames, the padding | ||
| 236 | will never produce duplicate plaintexts. | ||
| 237 | |||
| 238 | Symbolic link targets are considered a type of filename and are | ||
| 239 | encrypted in the same way as filenames in directory entries. Each | ||
| 240 | symlink also uses a unique key; hence, the hardcoded IV is not a | ||
| 241 | problem for symlinks. | ||
| 242 | |||
| 243 | User API | ||
| 244 | ======== | ||
| 245 | |||
| 246 | Setting an encryption policy | ||
| 247 | ---------------------------- | ||
| 248 | |||
| 249 | The FS_IOC_SET_ENCRYPTION_POLICY ioctl sets an encryption policy on an | ||
| 250 | empty directory or verifies that a directory or regular file already | ||
| 251 | has the specified encryption policy. It takes in a pointer to a | ||
| 252 | :c:type:`struct fscrypt_policy`, defined as follows:: | ||
| 253 | |||
| 254 | #define FS_KEY_DESCRIPTOR_SIZE 8 | ||
| 255 | |||
| 256 | struct fscrypt_policy { | ||
| 257 | __u8 version; | ||
| 258 | __u8 contents_encryption_mode; | ||
| 259 | __u8 filenames_encryption_mode; | ||
| 260 | __u8 flags; | ||
| 261 | __u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
| 262 | }; | ||
| 263 | |||
| 264 | This structure must be initialized as follows: | ||
| 265 | |||
| 266 | - ``version`` must be 0. | ||
| 267 | |||
| 268 | - ``contents_encryption_mode`` and ``filenames_encryption_mode`` must | ||
| 269 | be set to constants from ``<linux/fs.h>`` which identify the | ||
| 270 | encryption modes to use. If unsure, use | ||
| 271 | FS_ENCRYPTION_MODE_AES_256_XTS (1) for ``contents_encryption_mode`` | ||
| 272 | and FS_ENCRYPTION_MODE_AES_256_CTS (4) for | ||
| 273 | ``filenames_encryption_mode``. | ||
| 274 | |||
| 275 | - ``flags`` must be set to a value from ``<linux/fs.h>`` which | ||
| 276 | identifies the amount of NUL-padding to use when encrypting | ||
| 277 | filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3). | ||
| 278 | |||
| 279 | - ``master_key_descriptor`` specifies how to find the master key in | ||
| 280 | the keyring; see `Adding keys`_. It is up to userspace to choose a | ||
| 281 | unique ``master_key_descriptor`` for each master key. The e4crypt | ||
| 282 | and fscrypt tools use the first 8 bytes of | ||
| 283 | ``SHA-512(SHA-512(master_key))``, but this particular scheme is not | ||
| 284 | required. Also, the master key need not be in the keyring yet when | ||
| 285 | FS_IOC_SET_ENCRYPTION_POLICY is executed. However, it must be added | ||
| 286 | before any files can be created in the encrypted directory. | ||
| 287 | |||
| 288 | If the file is not yet encrypted, then FS_IOC_SET_ENCRYPTION_POLICY | ||
| 289 | verifies that the file is an empty directory. If so, the specified | ||
| 290 | encryption policy is assigned to the directory, turning it into an | ||
| 291 | encrypted directory. After that, and after providing the | ||
| 292 | corresponding master key as described in `Adding keys`_, all regular | ||
| 293 | files, directories (recursively), and symlinks created in the | ||
| 294 | directory will be encrypted, inheriting the same encryption policy. | ||
| 295 | The filenames in the directory's entries will be encrypted as well. | ||
| 296 | |||
| 297 | Alternatively, if the file is already encrypted, then | ||
| 298 | FS_IOC_SET_ENCRYPTION_POLICY validates that the specified encryption | ||
| 299 | policy exactly matches the actual one. If they match, then the ioctl | ||
| 300 | returns 0. Otherwise, it fails with EEXIST. This works on both | ||
| 301 | regular files and directories, including nonempty directories. | ||
| 302 | |||
| 303 | Note that the ext4 filesystem does not allow the root directory to be | ||
| 304 | encrypted, even if it is empty. Users who want to encrypt an entire | ||
| 305 | filesystem with one key should consider using dm-crypt instead. | ||
| 306 | |||
| 307 | FS_IOC_SET_ENCRYPTION_POLICY can fail with the following errors: | ||
| 308 | |||
| 309 | - ``EACCES``: the file is not owned by the process's uid, nor does the | ||
| 310 | process have the CAP_FOWNER capability in a namespace with the file | ||
| 311 | owner's uid mapped | ||
| 312 | - ``EEXIST``: the file is already encrypted with an encryption policy | ||
| 313 | different from the one specified | ||
| 314 | - ``EINVAL``: an invalid encryption policy was specified (invalid | ||
| 315 | version, mode(s), or flags) | ||
| 316 | - ``ENOTDIR``: the file is unencrypted and is a regular file, not a | ||
| 317 | directory | ||
| 318 | - ``ENOTEMPTY``: the file is unencrypted and is a nonempty directory | ||
| 319 | - ``ENOTTY``: this type of filesystem does not implement encryption | ||
| 320 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | ||
| 321 | support for this filesystem, or the filesystem superblock has not | ||
| 322 | had encryption enabled on it. (For example, to use encryption on an | ||
| 323 | ext4 filesystem, CONFIG_EXT4_ENCRYPTION must be enabled in the | ||
| 324 | kernel config, and the superblock must have had the "encrypt" | ||
| 325 | feature flag enabled using ``tune2fs -O encrypt`` or ``mkfs.ext4 -O | ||
| 326 | encrypt``.) | ||
| 327 | - ``EPERM``: this directory may not be encrypted, e.g. because it is | ||
| 328 | the root directory of an ext4 filesystem | ||
| 329 | - ``EROFS``: the filesystem is readonly | ||
| 330 | |||
| 331 | Getting an encryption policy | ||
| 332 | ---------------------------- | ||
| 333 | |||
| 334 | The FS_IOC_GET_ENCRYPTION_POLICY ioctl retrieves the :c:type:`struct | ||
| 335 | fscrypt_policy`, if any, for a directory or regular file. See above | ||
| 336 | for the struct definition. No additional permissions are required | ||
| 337 | beyond the ability to open the file. | ||
| 338 | |||
| 339 | FS_IOC_GET_ENCRYPTION_POLICY can fail with the following errors: | ||
| 340 | |||
| 341 | - ``EINVAL``: the file is encrypted, but it uses an unrecognized | ||
| 342 | encryption context format | ||
| 343 | - ``ENODATA``: the file is not encrypted | ||
| 344 | - ``ENOTTY``: this type of filesystem does not implement encryption | ||
| 345 | - ``EOPNOTSUPP``: the kernel was not configured with encryption | ||
| 346 | support for this filesystem | ||
| 347 | |||
| 348 | Note: if you only need to know whether a file is encrypted or not, on | ||
| 349 | most filesystems it is also possible to use the FS_IOC_GETFLAGS ioctl | ||
| 350 | and check for FS_ENCRYPT_FL, or to use the statx() system call and | ||
| 351 | check for STATX_ATTR_ENCRYPTED in stx_attributes. | ||
| 352 | |||
| 353 | Getting the per-filesystem salt | ||
| 354 | ------------------------------- | ||
| 355 | |||
| 356 | Some filesystems, such as ext4 and F2FS, also support the deprecated | ||
| 357 | ioctl FS_IOC_GET_ENCRYPTION_PWSALT. This ioctl retrieves a randomly | ||
| 358 | generated 16-byte value stored in the filesystem superblock. This | ||
| 359 | value is intended to used as a salt when deriving an encryption key | ||
| 360 | from a passphrase or other low-entropy user credential. | ||
| 361 | |||
| 362 | FS_IOC_GET_ENCRYPTION_PWSALT is deprecated. Instead, prefer to | ||
| 363 | generate and manage any needed salt(s) in userspace. | ||
| 364 | |||
| 365 | Adding keys | ||
| 366 | ----------- | ||
| 367 | |||
| 368 | To provide a master key, userspace must add it to an appropriate | ||
| 369 | keyring using the add_key() system call (see: | ||
| 370 | ``Documentation/security/keys/core.rst``). The key type must be | ||
| 371 | "logon"; keys of this type are kept in kernel memory and cannot be | ||
| 372 | read back by userspace. The key description must be "fscrypt:" | ||
| 373 | followed by the 16-character lower case hex representation of the | ||
| 374 | ``master_key_descriptor`` that was set in the encryption policy. The | ||
| 375 | key payload must conform to the following structure:: | ||
| 376 | |||
| 377 | #define FS_MAX_KEY_SIZE 64 | ||
| 378 | |||
| 379 | struct fscrypt_key { | ||
| 380 | u32 mode; | ||
| 381 | u8 raw[FS_MAX_KEY_SIZE]; | ||
| 382 | u32 size; | ||
| 383 | }; | ||
| 384 | |||
| 385 | ``mode`` is ignored; just set it to 0. The actual key is provided in | ||
| 386 | ``raw`` with ``size`` indicating its size in bytes. That is, the | ||
| 387 | bytes ``raw[0..size-1]`` (inclusive) are the actual key. | ||
| 388 | |||
| 389 | The key description prefix "fscrypt:" may alternatively be replaced | ||
| 390 | with a filesystem-specific prefix such as "ext4:". However, the | ||
| 391 | filesystem-specific prefixes are deprecated and should not be used in | ||
| 392 | new programs. | ||
| 393 | |||
| 394 | There are several different types of keyrings in which encryption keys | ||
| 395 | may be placed, such as a session keyring, a user session keyring, or a | ||
| 396 | user keyring. Each key must be placed in a keyring that is "attached" | ||
| 397 | to all processes that might need to access files encrypted with it, in | ||
| 398 | the sense that request_key() will find the key. Generally, if only | ||
| 399 | processes belonging to a specific user need to access a given | ||
| 400 | encrypted directory and no session keyring has been installed, then | ||
| 401 | that directory's key should be placed in that user's user session | ||
| 402 | keyring or user keyring. Otherwise, a session keyring should be | ||
| 403 | installed if needed, and the key should be linked into that session | ||
| 404 | keyring, or in a keyring linked into that session keyring. | ||
| 405 | |||
| 406 | Note: introducing the complex visibility semantics of keyrings here | ||
| 407 | was arguably a mistake --- especially given that by design, after any | ||
| 408 | process successfully opens an encrypted file (thereby setting up the | ||
| 409 | per-file key), possessing the keyring key is not actually required for | ||
| 410 | any process to read/write the file until its in-memory inode is | ||
| 411 | evicted. In the future there probably should be a way to provide keys | ||
| 412 | directly to the filesystem instead, which would make the intended | ||
| 413 | semantics clearer. | ||
| 414 | |||
| 415 | Access semantics | ||
| 416 | ================ | ||
| 417 | |||
| 418 | With the key | ||
| 419 | ------------ | ||
| 420 | |||
| 421 | With the encryption key, encrypted regular files, directories, and | ||
| 422 | symlinks behave very similarly to their unencrypted counterparts --- | ||
| 423 | after all, the encryption is intended to be transparent. However, | ||
| 424 | astute users may notice some differences in behavior: | ||
| 425 | |||
| 426 | - Unencrypted files, or files encrypted with a different encryption | ||
| 427 | policy (i.e. different key, modes, or flags), cannot be renamed or | ||
| 428 | linked into an encrypted directory; see `Encryption policy | ||
| 429 | enforcement`_. Attempts to do so will fail with EPERM. However, | ||
| 430 | encrypted files can be renamed within an encrypted directory, or | ||
| 431 | into an unencrypted directory. | ||
| 432 | |||
| 433 | - Direct I/O is not supported on encrypted files. Attempts to use | ||
| 434 | direct I/O on such files will fall back to buffered I/O. | ||
| 435 | |||
| 436 | - The fallocate operations FALLOC_FL_COLLAPSE_RANGE, | ||
| 437 | FALLOC_FL_INSERT_RANGE, and FALLOC_FL_ZERO_RANGE are not supported | ||
| 438 | on encrypted files and will fail with EOPNOTSUPP. | ||
| 439 | |||
| 440 | - Online defragmentation of encrypted files is not supported. The | ||
| 441 | EXT4_IOC_MOVE_EXT and F2FS_IOC_MOVE_RANGE ioctls will fail with | ||
| 442 | EOPNOTSUPP. | ||
| 443 | |||
| 444 | - The ext4 filesystem does not support data journaling with encrypted | ||
| 445 | regular files. It will fall back to ordered data mode instead. | ||
| 446 | |||
| 447 | - DAX (Direct Access) is not supported on encrypted files. | ||
| 448 | |||
| 449 | - The st_size of an encrypted symlink will not necessarily give the | ||
| 450 | length of the symlink target as required by POSIX. It will actually | ||
| 451 | give the length of the ciphertext, which may be slightly longer than | ||
| 452 | the plaintext due to the NUL-padding. | ||
| 453 | |||
| 454 | Note that mmap *is* supported. This is possible because the pagecache | ||
| 455 | for an encrypted file contains the plaintext, not the ciphertext. | ||
| 456 | |||
| 457 | Without the key | ||
| 458 | --------------- | ||
| 459 | |||
| 460 | Some filesystem operations may be performed on encrypted regular | ||
| 461 | files, directories, and symlinks even before their encryption key has | ||
| 462 | been provided: | ||
| 463 | |||
| 464 | - File metadata may be read, e.g. using stat(). | ||
| 465 | |||
| 466 | - Directories may be listed, in which case the filenames will be | ||
| 467 | listed in an encoded form derived from their ciphertext. The | ||
| 468 | current encoding algorithm is described in `Filename hashing and | ||
| 469 | encoding`_. The algorithm is subject to change, but it is | ||
| 470 | guaranteed that the presented filenames will be no longer than | ||
| 471 | NAME_MAX bytes, will not contain the ``/`` or ``\0`` characters, and | ||
| 472 | will uniquely identify directory entries. | ||
| 473 | |||
| 474 | The ``.`` and ``..`` directory entries are special. They are always | ||
| 475 | present and are not encrypted or encoded. | ||
| 476 | |||
| 477 | - Files may be deleted. That is, nondirectory files may be deleted | ||
| 478 | with unlink() as usual, and empty directories may be deleted with | ||
| 479 | rmdir() as usual. Therefore, ``rm`` and ``rm -r`` will work as | ||
| 480 | expected. | ||
| 481 | |||
| 482 | - Symlink targets may be read and followed, but they will be presented | ||
| 483 | in encrypted form, similar to filenames in directories. Hence, they | ||
| 484 | are unlikely to point to anywhere useful. | ||
| 485 | |||
| 486 | Without the key, regular files cannot be opened or truncated. | ||
| 487 | Attempts to do so will fail with ENOKEY. This implies that any | ||
| 488 | regular file operations that require a file descriptor, such as | ||
| 489 | read(), write(), mmap(), fallocate(), and ioctl(), are also forbidden. | ||
| 490 | |||
| 491 | Also without the key, files of any type (including directories) cannot | ||
| 492 | be created or linked into an encrypted directory, nor can a name in an | ||
| 493 | encrypted directory be the source or target of a rename, nor can an | ||
| 494 | O_TMPFILE temporary file be created in an encrypted directory. All | ||
| 495 | such operations will fail with ENOKEY. | ||
| 496 | |||
| 497 | It is not currently possible to backup and restore encrypted files | ||
| 498 | without the encryption key. This would require special APIs which | ||
| 499 | have not yet been implemented. | ||
| 500 | |||
| 501 | Encryption policy enforcement | ||
| 502 | ============================= | ||
| 503 | |||
| 504 | After an encryption policy has been set on a directory, all regular | ||
| 505 | files, directories, and symbolic links created in that directory | ||
| 506 | (recursively) will inherit that encryption policy. Special files --- | ||
| 507 | that is, named pipes, device nodes, and UNIX domain sockets --- will | ||
| 508 | not be encrypted. | ||
| 509 | |||
| 510 | Except for those special files, it is forbidden to have unencrypted | ||
| 511 | files, or files encrypted with a different encryption policy, in an | ||
| 512 | encrypted directory tree. Attempts to link or rename such a file into | ||
| 513 | an encrypted directory will fail with EPERM. This is also enforced | ||
| 514 | during ->lookup() to provide limited protection against offline | ||
| 515 | attacks that try to disable or downgrade encryption in known locations | ||
| 516 | where applications may later write sensitive data. It is recommended | ||
| 517 | that systems implementing a form of "verified boot" take advantage of | ||
| 518 | this by validating all top-level encryption policies prior to access. | ||
| 519 | |||
| 520 | Implementation details | ||
| 521 | ====================== | ||
| 522 | |||
| 523 | Encryption context | ||
| 524 | ------------------ | ||
| 525 | |||
| 526 | An encryption policy is represented on-disk by a :c:type:`struct | ||
| 527 | fscrypt_context`. It is up to individual filesystems to decide where | ||
| 528 | to store it, but normally it would be stored in a hidden extended | ||
| 529 | attribute. It should *not* be exposed by the xattr-related system | ||
| 530 | calls such as getxattr() and setxattr() because of the special | ||
| 531 | semantics of the encryption xattr. (In particular, there would be | ||
| 532 | much confusion if an encryption policy were to be added to or removed | ||
| 533 | from anything other than an empty directory.) The struct is defined | ||
| 534 | as follows:: | ||
| 535 | |||
| 536 | #define FS_KEY_DESCRIPTOR_SIZE 8 | ||
| 537 | #define FS_KEY_DERIVATION_NONCE_SIZE 16 | ||
| 538 | |||
| 539 | struct fscrypt_context { | ||
| 540 | u8 format; | ||
| 541 | u8 contents_encryption_mode; | ||
| 542 | u8 filenames_encryption_mode; | ||
| 543 | u8 flags; | ||
| 544 | u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE]; | ||
| 545 | u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE]; | ||
| 546 | }; | ||
| 547 | |||
| 548 | Note that :c:type:`struct fscrypt_context` contains the same | ||
| 549 | information as :c:type:`struct fscrypt_policy` (see `Setting an | ||
| 550 | encryption policy`_), except that :c:type:`struct fscrypt_context` | ||
| 551 | also contains a nonce. The nonce is randomly generated by the kernel | ||
| 552 | and is used to derive the inode's encryption key as described in | ||
| 553 | `Per-file keys`_. | ||
| 554 | |||
| 555 | Data path changes | ||
| 556 | ----------------- | ||
| 557 | |||
| 558 | For the read path (->readpage()) of regular files, filesystems can | ||
| 559 | read the ciphertext into the page cache and decrypt it in-place. The | ||
| 560 | page lock must be held until decryption has finished, to prevent the | ||
| 561 | page from becoming visible to userspace prematurely. | ||
| 562 | |||
| 563 | For the write path (->writepage()) of regular files, filesystems | ||
| 564 | cannot encrypt data in-place in the page cache, since the cached | ||
| 565 | plaintext must be preserved. Instead, filesystems must encrypt into a | ||
| 566 | temporary buffer or "bounce page", then write out the temporary | ||
| 567 | buffer. Some filesystems, such as UBIFS, already use temporary | ||
| 568 | buffers regardless of encryption. Other filesystems, such as ext4 and | ||
| 569 | F2FS, have to allocate bounce pages specially for encryption. | ||
| 570 | |||
| 571 | Filename hashing and encoding | ||
| 572 | ----------------------------- | ||
| 573 | |||
| 574 | Modern filesystems accelerate directory lookups by using indexed | ||
| 575 | directories. An indexed directory is organized as a tree keyed by | ||
| 576 | filename hashes. When a ->lookup() is requested, the filesystem | ||
| 577 | normally hashes the filename being looked up so that it can quickly | ||
| 578 | find the corresponding directory entry, if any. | ||
| 579 | |||
| 580 | With encryption, lookups must be supported and efficient both with and | ||
| 581 | without the encryption key. Clearly, it would not work to hash the | ||
| 582 | plaintext filenames, since the plaintext filenames are unavailable | ||
| 583 | without the key. (Hashing the plaintext filenames would also make it | ||
| 584 | impossible for the filesystem's fsck tool to optimize encrypted | ||
| 585 | directories.) Instead, filesystems hash the ciphertext filenames, | ||
| 586 | i.e. the bytes actually stored on-disk in the directory entries. When | ||
| 587 | asked to do a ->lookup() with the key, the filesystem just encrypts | ||
| 588 | the user-supplied name to get the ciphertext. | ||
| 589 | |||
| 590 | Lookups without the key are more complicated. The raw ciphertext may | ||
| 591 | contain the ``\0`` and ``/`` characters, which are illegal in | ||
| 592 | filenames. Therefore, readdir() must base64-encode the ciphertext for | ||
| 593 | presentation. For most filenames, this works fine; on ->lookup(), the | ||
| 594 | filesystem just base64-decodes the user-supplied name to get back to | ||
| 595 | the raw ciphertext. | ||
| 596 | |||
| 597 | However, for very long filenames, base64 encoding would cause the | ||
| 598 | filename length to exceed NAME_MAX. To prevent this, readdir() | ||
| 599 | actually presents long filenames in an abbreviated form which encodes | ||
| 600 | a strong "hash" of the ciphertext filename, along with the optional | ||
| 601 | filesystem-specific hash(es) needed for directory lookups. This | ||
| 602 | allows the filesystem to still, with a high degree of confidence, map | ||
| 603 | the filename given in ->lookup() back to a particular directory entry | ||
| 604 | that was previously listed by readdir(). See :c:type:`struct | ||
| 605 | fscrypt_digested_name` in the source for more details. | ||
| 606 | |||
| 607 | Note that the precise way that filenames are presented to userspace | ||
| 608 | without the key is subject to change in the future. It is only meant | ||
| 609 | as a way to temporarily present valid filenames so that commands like | ||
| 610 | ``rm -r`` work as expected on encrypted directories. | ||
diff --git a/Documentation/filesystems/index.rst b/Documentation/filesystems/index.rst index 256e10eedba4..53b89d0edc15 100644 --- a/Documentation/filesystems/index.rst +++ b/Documentation/filesystems/index.rst | |||
| @@ -315,3 +315,14 @@ exported for use by modules. | |||
| 315 | :internal: | 315 | :internal: |
| 316 | 316 | ||
| 317 | .. kernel-doc:: fs/pipe.c | 317 | .. kernel-doc:: fs/pipe.c |
| 318 | |||
| 319 | Encryption API | ||
| 320 | ============== | ||
| 321 | |||
| 322 | A library which filesystems can hook into to support transparent | ||
| 323 | encryption of files and directories. | ||
| 324 | |||
| 325 | .. toctree:: | ||
| 326 | :maxdepth: 2 | ||
| 327 | |||
| 328 | fscrypt | ||
diff --git a/MAINTAINERS b/MAINTAINERS index 9a9343a24528..e372994747b7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS | |||
| @@ -5664,6 +5664,7 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tytso/fscrypt.git | |||
| 5664 | S: Supported | 5664 | S: Supported |
| 5665 | F: fs/crypto/ | 5665 | F: fs/crypto/ |
| 5666 | F: include/linux/fscrypt*.h | 5666 | F: include/linux/fscrypt*.h |
| 5667 | F: Documentation/filesystems/fscrypt.rst | ||
| 5667 | 5668 | ||
| 5668 | FUJITSU FR-V (FRV) PORT | 5669 | FUJITSU FR-V (FRV) PORT |
| 5669 | S: Orphan | 5670 | S: Orphan |
diff --git a/fs/crypto/Makefile b/fs/crypto/Makefile index 9f6607f17b53..cb496989a6b6 100644 --- a/fs/crypto/Makefile +++ b/fs/crypto/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | obj-$(CONFIG_FS_ENCRYPTION) += fscrypto.o | 1 | obj-$(CONFIG_FS_ENCRYPTION) += fscrypto.o |
| 2 | 2 | ||
| 3 | fscrypto-y := crypto.o fname.o policy.o keyinfo.o | 3 | fscrypto-y := crypto.o fname.o hooks.o keyinfo.o policy.o |
| 4 | fscrypto-$(CONFIG_BLOCK) += bio.o | 4 | fscrypto-$(CONFIG_BLOCK) += bio.o |
diff --git a/fs/crypto/crypto.c b/fs/crypto/crypto.c index 80a3cada53de..732a786cce9d 100644 --- a/fs/crypto/crypto.c +++ b/fs/crypto/crypto.c | |||
| @@ -320,7 +320,7 @@ static int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags) | |||
| 320 | return -ECHILD; | 320 | return -ECHILD; |
| 321 | 321 | ||
| 322 | dir = dget_parent(dentry); | 322 | dir = dget_parent(dentry); |
| 323 | if (!d_inode(dir)->i_sb->s_cop->is_encrypted(d_inode(dir))) { | 323 | if (!IS_ENCRYPTED(d_inode(dir))) { |
| 324 | dput(dir); | 324 | dput(dir); |
| 325 | return 0; | 325 | return 0; |
| 326 | } | 326 | } |
| @@ -390,11 +390,8 @@ int fscrypt_initialize(unsigned int cop_flags) | |||
| 390 | { | 390 | { |
| 391 | int i, res = -ENOMEM; | 391 | int i, res = -ENOMEM; |
| 392 | 392 | ||
| 393 | /* | 393 | /* No need to allocate a bounce page pool if this FS won't use it. */ |
| 394 | * No need to allocate a bounce page pool if there already is one or | 394 | if (cop_flags & FS_CFLG_OWN_PAGES) |
| 395 | * this FS won't use it. | ||
| 396 | */ | ||
| 397 | if (cop_flags & FS_CFLG_OWN_PAGES || fscrypt_bounce_page_pool) | ||
| 398 | return 0; | 395 | return 0; |
| 399 | 396 | ||
| 400 | mutex_lock(&fscrypt_init_mutex); | 397 | mutex_lock(&fscrypt_init_mutex); |
diff --git a/fs/crypto/fname.c b/fs/crypto/fname.c index 0b94cd2b3222..305541bcd108 100644 --- a/fs/crypto/fname.c +++ b/fs/crypto/fname.c | |||
| @@ -359,8 +359,7 @@ int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname, | |||
| 359 | memset(fname, 0, sizeof(struct fscrypt_name)); | 359 | memset(fname, 0, sizeof(struct fscrypt_name)); |
| 360 | fname->usr_fname = iname; | 360 | fname->usr_fname = iname; |
| 361 | 361 | ||
| 362 | if (!dir->i_sb->s_cop->is_encrypted(dir) || | 362 | if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) { |
| 363 | fscrypt_is_dot_dotdot(iname)) { | ||
| 364 | fname->disk_name.name = (unsigned char *)iname->name; | 363 | fname->disk_name.name = (unsigned char *)iname->name; |
| 365 | fname->disk_name.len = iname->len; | 364 | fname->disk_name.len = iname->len; |
| 366 | return 0; | 365 | return 0; |
diff --git a/fs/crypto/fscrypt_private.h b/fs/crypto/fscrypt_private.h index e54e602b473f..c0b4f5597e1a 100644 --- a/fs/crypto/fscrypt_private.h +++ b/fs/crypto/fscrypt_private.h | |||
| @@ -12,7 +12,8 @@ | |||
| 12 | #ifndef _FSCRYPT_PRIVATE_H | 12 | #ifndef _FSCRYPT_PRIVATE_H |
| 13 | #define _FSCRYPT_PRIVATE_H | 13 | #define _FSCRYPT_PRIVATE_H |
| 14 | 14 | ||
| 15 | #include <linux/fscrypt_supp.h> | 15 | #define __FS_HAS_ENCRYPTION 1 |
| 16 | #include <linux/fscrypt.h> | ||
| 16 | #include <crypto/hash.h> | 17 | #include <crypto/hash.h> |
| 17 | 18 | ||
| 18 | /* Encryption parameters */ | 19 | /* Encryption parameters */ |
diff --git a/fs/crypto/hooks.c b/fs/crypto/hooks.c new file mode 100644 index 000000000000..9f5fb2eb9cf7 --- /dev/null +++ b/fs/crypto/hooks.c | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | /* | ||
| 2 | * fs/crypto/hooks.c | ||
| 3 | * | ||
| 4 | * Encryption hooks for higher-level filesystem operations. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include <linux/ratelimit.h> | ||
| 8 | #include "fscrypt_private.h" | ||
| 9 | |||
| 10 | /** | ||
| 11 | * fscrypt_file_open - prepare to open a possibly-encrypted regular file | ||
| 12 | * @inode: the inode being opened | ||
| 13 | * @filp: the struct file being set up | ||
| 14 | * | ||
| 15 | * Currently, an encrypted regular file can only be opened if its encryption key | ||
| 16 | * is available; access to the raw encrypted contents is not supported. | ||
| 17 | * Therefore, we first set up the inode's encryption key (if not already done) | ||
| 18 | * and return an error if it's unavailable. | ||
| 19 | * | ||
| 20 | * We also verify that if the parent directory (from the path via which the file | ||
| 21 | * is being opened) is encrypted, then the inode being opened uses the same | ||
| 22 | * encryption policy. This is needed as part of the enforcement that all files | ||
| 23 | * in an encrypted directory tree use the same encryption policy, as a | ||
| 24 | * protection against certain types of offline attacks. Note that this check is | ||
| 25 | * needed even when opening an *unencrypted* file, since it's forbidden to have | ||
| 26 | * an unencrypted file in an encrypted directory. | ||
| 27 | * | ||
| 28 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code | ||
| 29 | */ | ||
| 30 | int fscrypt_file_open(struct inode *inode, struct file *filp) | ||
| 31 | { | ||
| 32 | int err; | ||
| 33 | struct dentry *dir; | ||
| 34 | |||
| 35 | err = fscrypt_require_key(inode); | ||
| 36 | if (err) | ||
| 37 | return err; | ||
| 38 | |||
| 39 | dir = dget_parent(file_dentry(filp)); | ||
| 40 | if (IS_ENCRYPTED(d_inode(dir)) && | ||
| 41 | !fscrypt_has_permitted_context(d_inode(dir), inode)) { | ||
| 42 | pr_warn_ratelimited("fscrypt: inconsistent encryption contexts: %lu/%lu", | ||
| 43 | d_inode(dir)->i_ino, inode->i_ino); | ||
| 44 | err = -EPERM; | ||
| 45 | } | ||
| 46 | dput(dir); | ||
| 47 | return err; | ||
| 48 | } | ||
| 49 | EXPORT_SYMBOL_GPL(fscrypt_file_open); | ||
| 50 | |||
| 51 | int __fscrypt_prepare_link(struct inode *inode, struct inode *dir) | ||
| 52 | { | ||
| 53 | int err; | ||
| 54 | |||
| 55 | err = fscrypt_require_key(dir); | ||
| 56 | if (err) | ||
| 57 | return err; | ||
| 58 | |||
| 59 | if (!fscrypt_has_permitted_context(dir, inode)) | ||
| 60 | return -EPERM; | ||
| 61 | |||
| 62 | return 0; | ||
| 63 | } | ||
| 64 | EXPORT_SYMBOL_GPL(__fscrypt_prepare_link); | ||
| 65 | |||
| 66 | int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry, | ||
| 67 | struct inode *new_dir, struct dentry *new_dentry, | ||
| 68 | unsigned int flags) | ||
| 69 | { | ||
| 70 | int err; | ||
| 71 | |||
| 72 | err = fscrypt_require_key(old_dir); | ||
| 73 | if (err) | ||
| 74 | return err; | ||
| 75 | |||
| 76 | err = fscrypt_require_key(new_dir); | ||
| 77 | if (err) | ||
| 78 | return err; | ||
| 79 | |||
| 80 | if (old_dir != new_dir) { | ||
| 81 | if (IS_ENCRYPTED(new_dir) && | ||
| 82 | !fscrypt_has_permitted_context(new_dir, | ||
| 83 | d_inode(old_dentry))) | ||
| 84 | return -EPERM; | ||
| 85 | |||
| 86 | if ((flags & RENAME_EXCHANGE) && | ||
| 87 | IS_ENCRYPTED(old_dir) && | ||
| 88 | !fscrypt_has_permitted_context(old_dir, | ||
| 89 | d_inode(new_dentry))) | ||
| 90 | return -EPERM; | ||
| 91 | } | ||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | EXPORT_SYMBOL_GPL(__fscrypt_prepare_rename); | ||
| 95 | |||
| 96 | int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry) | ||
| 97 | { | ||
| 98 | int err = fscrypt_get_encryption_info(dir); | ||
| 99 | |||
| 100 | if (err) | ||
| 101 | return err; | ||
| 102 | |||
| 103 | if (fscrypt_has_encryption_key(dir)) { | ||
| 104 | spin_lock(&dentry->d_lock); | ||
| 105 | dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY; | ||
| 106 | spin_unlock(&dentry->d_lock); | ||
| 107 | } | ||
| 108 | |||
| 109 | d_set_d_op(dentry, &fscrypt_d_ops); | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | EXPORT_SYMBOL_GPL(__fscrypt_prepare_lookup); | ||
diff --git a/fs/crypto/keyinfo.c b/fs/crypto/keyinfo.c index 4486b3eecc27..5e6e846f5a24 100644 --- a/fs/crypto/keyinfo.c +++ b/fs/crypto/keyinfo.c | |||
| @@ -259,7 +259,7 @@ int fscrypt_get_encryption_info(struct inode *inode) | |||
| 259 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | 259 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 260 | if (res < 0) { | 260 | if (res < 0) { |
| 261 | if (!fscrypt_dummy_context_enabled(inode) || | 261 | if (!fscrypt_dummy_context_enabled(inode) || |
| 262 | inode->i_sb->s_cop->is_encrypted(inode)) | 262 | IS_ENCRYPTED(inode)) |
| 263 | return res; | 263 | return res; |
| 264 | /* Fake up a context for an unencrypted directory */ | 264 | /* Fake up a context for an unencrypted directory */ |
| 265 | memset(&ctx, 0, sizeof(ctx)); | 265 | memset(&ctx, 0, sizeof(ctx)); |
diff --git a/fs/crypto/policy.c b/fs/crypto/policy.c index a120649beeca..c6d431a5cce9 100644 --- a/fs/crypto/policy.c +++ b/fs/crypto/policy.c | |||
| @@ -110,7 +110,7 @@ int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) | |||
| 110 | struct fscrypt_policy policy; | 110 | struct fscrypt_policy policy; |
| 111 | int res; | 111 | int res; |
| 112 | 112 | ||
| 113 | if (!inode->i_sb->s_cop->is_encrypted(inode)) | 113 | if (!IS_ENCRYPTED(inode)) |
| 114 | return -ENODATA; | 114 | return -ENODATA; |
| 115 | 115 | ||
| 116 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); | 116 | res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| @@ -167,11 +167,11 @@ int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) | |||
| 167 | return 1; | 167 | return 1; |
| 168 | 168 | ||
| 169 | /* No restrictions if the parent directory is unencrypted */ | 169 | /* No restrictions if the parent directory is unencrypted */ |
| 170 | if (!cops->is_encrypted(parent)) | 170 | if (!IS_ENCRYPTED(parent)) |
| 171 | return 1; | 171 | return 1; |
| 172 | 172 | ||
| 173 | /* Encrypted directories must not contain unencrypted files */ | 173 | /* Encrypted directories must not contain unencrypted files */ |
| 174 | if (!cops->is_encrypted(child)) | 174 | if (!IS_ENCRYPTED(child)) |
| 175 | return 0; | 175 | return 0; |
| 176 | 176 | ||
| 177 | /* | 177 | /* |
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 58a0304566db..27f38bb5046d 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h | |||
| @@ -34,17 +34,15 @@ | |||
| 34 | #include <linux/percpu_counter.h> | 34 | #include <linux/percpu_counter.h> |
| 35 | #include <linux/ratelimit.h> | 35 | #include <linux/ratelimit.h> |
| 36 | #include <crypto/hash.h> | 36 | #include <crypto/hash.h> |
| 37 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | ||
| 38 | #include <linux/fscrypt_supp.h> | ||
| 39 | #else | ||
| 40 | #include <linux/fscrypt_notsupp.h> | ||
| 41 | #endif | ||
| 42 | #include <linux/falloc.h> | 37 | #include <linux/falloc.h> |
| 43 | #include <linux/percpu-rwsem.h> | 38 | #include <linux/percpu-rwsem.h> |
| 44 | #ifdef __KERNEL__ | 39 | #ifdef __KERNEL__ |
| 45 | #include <linux/compat.h> | 40 | #include <linux/compat.h> |
| 46 | #endif | 41 | #endif |
| 47 | 42 | ||
| 43 | #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_EXT4_FS_ENCRYPTION) | ||
| 44 | #include <linux/fscrypt.h> | ||
| 45 | |||
| 48 | /* | 46 | /* |
| 49 | * The fourth extended filesystem constants/structures | 47 | * The fourth extended filesystem constants/structures |
| 50 | */ | 48 | */ |
diff --git a/fs/ext4/file.c b/fs/ext4/file.c index 5cb9aa3ad249..b937078bcff3 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c | |||
| @@ -365,7 +365,6 @@ static int ext4_file_open(struct inode * inode, struct file * filp) | |||
| 365 | struct super_block *sb = inode->i_sb; | 365 | struct super_block *sb = inode->i_sb; |
| 366 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 366 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 367 | struct vfsmount *mnt = filp->f_path.mnt; | 367 | struct vfsmount *mnt = filp->f_path.mnt; |
| 368 | struct dentry *dir; | ||
| 369 | struct path path; | 368 | struct path path; |
| 370 | char buf[64], *cp; | 369 | char buf[64], *cp; |
| 371 | int ret; | 370 | int ret; |
| @@ -405,25 +404,11 @@ static int ext4_file_open(struct inode * inode, struct file * filp) | |||
| 405 | ext4_journal_stop(handle); | 404 | ext4_journal_stop(handle); |
| 406 | } | 405 | } |
| 407 | } | 406 | } |
| 408 | if (ext4_encrypted_inode(inode)) { | ||
| 409 | ret = fscrypt_get_encryption_info(inode); | ||
| 410 | if (ret) | ||
| 411 | return -EACCES; | ||
| 412 | if (!fscrypt_has_encryption_key(inode)) | ||
| 413 | return -ENOKEY; | ||
| 414 | } | ||
| 415 | 407 | ||
| 416 | dir = dget_parent(file_dentry(filp)); | 408 | ret = fscrypt_file_open(inode, filp); |
| 417 | if (ext4_encrypted_inode(d_inode(dir)) && | 409 | if (ret) |
| 418 | !fscrypt_has_permitted_context(d_inode(dir), inode)) { | 410 | return ret; |
| 419 | ext4_warning(inode->i_sb, | 411 | |
| 420 | "Inconsistent encryption contexts: %lu/%lu", | ||
| 421 | (unsigned long) d_inode(dir)->i_ino, | ||
| 422 | (unsigned long) inode->i_ino); | ||
| 423 | dput(dir); | ||
| 424 | return -EPERM; | ||
| 425 | } | ||
| 426 | dput(dir); | ||
| 427 | /* | 412 | /* |
| 428 | * Set up the jbd2_inode if we are opening the inode for | 413 | * Set up the jbd2_inode if we are opening the inode for |
| 429 | * writing and the journal is present | 414 | * writing and the journal is present |
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 90afeb7293a6..168a1b499cdf 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c | |||
| @@ -4590,10 +4590,13 @@ void ext4_set_inode_flags(struct inode *inode) | |||
| 4590 | new_fl |= S_DIRSYNC; | 4590 | new_fl |= S_DIRSYNC; |
| 4591 | if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode) && | 4591 | if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode) && |
| 4592 | !ext4_should_journal_data(inode) && !ext4_has_inline_data(inode) && | 4592 | !ext4_should_journal_data(inode) && !ext4_has_inline_data(inode) && |
| 4593 | !ext4_encrypted_inode(inode)) | 4593 | !(flags & EXT4_ENCRYPT_FL)) |
| 4594 | new_fl |= S_DAX; | 4594 | new_fl |= S_DAX; |
| 4595 | if (flags & EXT4_ENCRYPT_FL) | ||
| 4596 | new_fl |= S_ENCRYPTED; | ||
| 4595 | inode_set_flags(inode, new_fl, | 4597 | inode_set_flags(inode, new_fl, |
| 4596 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX); | 4598 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX| |
| 4599 | S_ENCRYPTED); | ||
| 4597 | } | 4600 | } |
| 4598 | 4601 | ||
| 4599 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, | 4602 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
| @@ -5309,6 +5312,10 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr) | |||
| 5309 | if (error) | 5312 | if (error) |
| 5310 | return error; | 5313 | return error; |
| 5311 | 5314 | ||
| 5315 | error = fscrypt_prepare_setattr(dentry, attr); | ||
| 5316 | if (error) | ||
| 5317 | return error; | ||
| 5318 | |||
| 5312 | if (is_quota_modification(inode, attr)) { | 5319 | if (is_quota_modification(inode, attr)) { |
| 5313 | error = dquot_initialize(inode); | 5320 | error = dquot_initialize(inode); |
| 5314 | if (error) | 5321 | if (error) |
| @@ -5354,14 +5361,6 @@ int ext4_setattr(struct dentry *dentry, struct iattr *attr) | |||
| 5354 | loff_t oldsize = inode->i_size; | 5361 | loff_t oldsize = inode->i_size; |
| 5355 | int shrink = (attr->ia_size <= inode->i_size); | 5362 | int shrink = (attr->ia_size <= inode->i_size); |
| 5356 | 5363 | ||
| 5357 | if (ext4_encrypted_inode(inode)) { | ||
| 5358 | error = fscrypt_get_encryption_info(inode); | ||
| 5359 | if (error) | ||
| 5360 | return error; | ||
| 5361 | if (!fscrypt_has_encryption_key(inode)) | ||
| 5362 | return -ENOKEY; | ||
| 5363 | } | ||
| 5364 | |||
| 5365 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { | 5364 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
| 5366 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 5365 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 5367 | 5366 | ||
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index bd48a8d83961..798b3ac680db 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c | |||
| @@ -1539,24 +1539,14 @@ static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsi | |||
| 1539 | struct inode *inode; | 1539 | struct inode *inode; |
| 1540 | struct ext4_dir_entry_2 *de; | 1540 | struct ext4_dir_entry_2 *de; |
| 1541 | struct buffer_head *bh; | 1541 | struct buffer_head *bh; |
| 1542 | int err; | ||
| 1542 | 1543 | ||
| 1543 | if (ext4_encrypted_inode(dir)) { | 1544 | err = fscrypt_prepare_lookup(dir, dentry, flags); |
| 1544 | int res = fscrypt_get_encryption_info(dir); | 1545 | if (err) |
| 1545 | 1546 | return ERR_PTR(err); | |
| 1546 | /* | ||
| 1547 | * DCACHE_ENCRYPTED_WITH_KEY is set if the dentry is | ||
| 1548 | * created while the directory was encrypted and we | ||
| 1549 | * have access to the key. | ||
| 1550 | */ | ||
| 1551 | if (fscrypt_has_encryption_key(dir)) | ||
| 1552 | fscrypt_set_encrypted_dentry(dentry); | ||
| 1553 | fscrypt_set_d_op(dentry); | ||
| 1554 | if (res && res != -ENOKEY) | ||
| 1555 | return ERR_PTR(res); | ||
| 1556 | } | ||
| 1557 | 1547 | ||
| 1558 | if (dentry->d_name.len > EXT4_NAME_LEN) | 1548 | if (dentry->d_name.len > EXT4_NAME_LEN) |
| 1559 | return ERR_PTR(-ENAMETOOLONG); | 1549 | return ERR_PTR(-ENAMETOOLONG); |
| 1560 | 1550 | ||
| 1561 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); | 1551 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); |
| 1562 | if (IS_ERR(bh)) | 1552 | if (IS_ERR(bh)) |
| @@ -3222,9 +3212,10 @@ static int ext4_link(struct dentry *old_dentry, | |||
| 3222 | 3212 | ||
| 3223 | if (inode->i_nlink >= EXT4_LINK_MAX) | 3213 | if (inode->i_nlink >= EXT4_LINK_MAX) |
| 3224 | return -EMLINK; | 3214 | return -EMLINK; |
| 3225 | if (ext4_encrypted_inode(dir) && | 3215 | |
| 3226 | !fscrypt_has_permitted_context(dir, inode)) | 3216 | err = fscrypt_prepare_link(old_dentry, dir, dentry); |
| 3227 | return -EPERM; | 3217 | if (err) |
| 3218 | return err; | ||
| 3228 | 3219 | ||
| 3229 | if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) && | 3220 | if ((ext4_test_inode_flag(dir, EXT4_INODE_PROJINHERIT)) && |
| 3230 | (!projid_eq(EXT4_I(dir)->i_projid, | 3221 | (!projid_eq(EXT4_I(dir)->i_projid, |
| @@ -3516,12 +3507,6 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
| 3516 | EXT4_I(old_dentry->d_inode)->i_projid))) | 3507 | EXT4_I(old_dentry->d_inode)->i_projid))) |
| 3517 | return -EXDEV; | 3508 | return -EXDEV; |
| 3518 | 3509 | ||
| 3519 | if ((ext4_encrypted_inode(old_dir) && | ||
| 3520 | !fscrypt_has_encryption_key(old_dir)) || | ||
| 3521 | (ext4_encrypted_inode(new_dir) && | ||
| 3522 | !fscrypt_has_encryption_key(new_dir))) | ||
| 3523 | return -ENOKEY; | ||
| 3524 | |||
| 3525 | retval = dquot_initialize(old.dir); | 3510 | retval = dquot_initialize(old.dir); |
| 3526 | if (retval) | 3511 | if (retval) |
| 3527 | return retval; | 3512 | return retval; |
| @@ -3550,13 +3535,6 @@ static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
| 3550 | if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino) | 3535 | if (!old.bh || le32_to_cpu(old.de->inode) != old.inode->i_ino) |
| 3551 | goto end_rename; | 3536 | goto end_rename; |
| 3552 | 3537 | ||
| 3553 | if ((old.dir != new.dir) && | ||
| 3554 | ext4_encrypted_inode(new.dir) && | ||
| 3555 | !fscrypt_has_permitted_context(new.dir, old.inode)) { | ||
| 3556 | retval = -EPERM; | ||
| 3557 | goto end_rename; | ||
| 3558 | } | ||
| 3559 | |||
| 3560 | new.bh = ext4_find_entry(new.dir, &new.dentry->d_name, | 3538 | new.bh = ext4_find_entry(new.dir, &new.dentry->d_name, |
| 3561 | &new.de, &new.inlined); | 3539 | &new.de, &new.inlined); |
| 3562 | if (IS_ERR(new.bh)) { | 3540 | if (IS_ERR(new.bh)) { |
| @@ -3722,19 +3700,6 @@ static int ext4_cross_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
| 3722 | int retval; | 3700 | int retval; |
| 3723 | struct timespec ctime; | 3701 | struct timespec ctime; |
| 3724 | 3702 | ||
| 3725 | if ((ext4_encrypted_inode(old_dir) && | ||
| 3726 | !fscrypt_has_encryption_key(old_dir)) || | ||
| 3727 | (ext4_encrypted_inode(new_dir) && | ||
| 3728 | !fscrypt_has_encryption_key(new_dir))) | ||
| 3729 | return -ENOKEY; | ||
| 3730 | |||
| 3731 | if ((ext4_encrypted_inode(old_dir) || | ||
| 3732 | ext4_encrypted_inode(new_dir)) && | ||
| 3733 | (old_dir != new_dir) && | ||
| 3734 | (!fscrypt_has_permitted_context(new_dir, old.inode) || | ||
| 3735 | !fscrypt_has_permitted_context(old_dir, new.inode))) | ||
| 3736 | return -EPERM; | ||
| 3737 | |||
| 3738 | if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) && | 3703 | if ((ext4_test_inode_flag(new_dir, EXT4_INODE_PROJINHERIT) && |
| 3739 | !projid_eq(EXT4_I(new_dir)->i_projid, | 3704 | !projid_eq(EXT4_I(new_dir)->i_projid, |
| 3740 | EXT4_I(old_dentry->d_inode)->i_projid)) || | 3705 | EXT4_I(old_dentry->d_inode)->i_projid)) || |
| @@ -3861,12 +3826,19 @@ static int ext4_rename2(struct inode *old_dir, struct dentry *old_dentry, | |||
| 3861 | struct inode *new_dir, struct dentry *new_dentry, | 3826 | struct inode *new_dir, struct dentry *new_dentry, |
| 3862 | unsigned int flags) | 3827 | unsigned int flags) |
| 3863 | { | 3828 | { |
| 3829 | int err; | ||
| 3830 | |||
| 3864 | if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb)))) | 3831 | if (unlikely(ext4_forced_shutdown(EXT4_SB(old_dir->i_sb)))) |
| 3865 | return -EIO; | 3832 | return -EIO; |
| 3866 | 3833 | ||
| 3867 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) | 3834 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
| 3868 | return -EINVAL; | 3835 | return -EINVAL; |
| 3869 | 3836 | ||
| 3837 | err = fscrypt_prepare_rename(old_dir, old_dentry, new_dir, new_dentry, | ||
| 3838 | flags); | ||
| 3839 | if (err) | ||
| 3840 | return err; | ||
| 3841 | |||
| 3870 | if (flags & RENAME_EXCHANGE) { | 3842 | if (flags & RENAME_EXCHANGE) { |
| 3871 | return ext4_cross_rename(old_dir, old_dentry, | 3843 | return ext4_cross_rename(old_dir, old_dentry, |
| 3872 | new_dir, new_dentry); | 3844 | new_dir, new_dentry); |
diff --git a/fs/ext4/super.c b/fs/ext4/super.c index b0915b734a38..e2557711a11c 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c | |||
| @@ -1181,7 +1181,8 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len, | |||
| 1181 | ext4_clear_inode_state(inode, | 1181 | ext4_clear_inode_state(inode, |
| 1182 | EXT4_STATE_MAY_INLINE_DATA); | 1182 | EXT4_STATE_MAY_INLINE_DATA); |
| 1183 | /* | 1183 | /* |
| 1184 | * Update inode->i_flags - e.g. S_DAX may get disabled | 1184 | * Update inode->i_flags - S_ENCRYPTED will be enabled, |
| 1185 | * S_DAX may be disabled | ||
| 1185 | */ | 1186 | */ |
| 1186 | ext4_set_inode_flags(inode); | 1187 | ext4_set_inode_flags(inode); |
| 1187 | } | 1188 | } |
| @@ -1206,7 +1207,10 @@ retry: | |||
| 1206 | ctx, len, 0); | 1207 | ctx, len, 0); |
| 1207 | if (!res) { | 1208 | if (!res) { |
| 1208 | ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); | 1209 | ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT); |
| 1209 | /* Update inode->i_flags - e.g. S_DAX may get disabled */ | 1210 | /* |
| 1211 | * Update inode->i_flags - S_ENCRYPTED will be enabled, | ||
| 1212 | * S_DAX may be disabled | ||
| 1213 | */ | ||
| 1210 | ext4_set_inode_flags(inode); | 1214 | ext4_set_inode_flags(inode); |
| 1211 | res = ext4_mark_inode_dirty(handle, inode); | 1215 | res = ext4_mark_inode_dirty(handle, inode); |
| 1212 | if (res) | 1216 | if (res) |
| @@ -1237,14 +1241,9 @@ static const struct fscrypt_operations ext4_cryptops = { | |||
| 1237 | .get_context = ext4_get_context, | 1241 | .get_context = ext4_get_context, |
| 1238 | .set_context = ext4_set_context, | 1242 | .set_context = ext4_set_context, |
| 1239 | .dummy_context = ext4_dummy_context, | 1243 | .dummy_context = ext4_dummy_context, |
| 1240 | .is_encrypted = ext4_encrypted_inode, | ||
| 1241 | .empty_dir = ext4_empty_dir, | 1244 | .empty_dir = ext4_empty_dir, |
| 1242 | .max_namelen = ext4_max_namelen, | 1245 | .max_namelen = ext4_max_namelen, |
| 1243 | }; | 1246 | }; |
| 1244 | #else | ||
| 1245 | static const struct fscrypt_operations ext4_cryptops = { | ||
| 1246 | .is_encrypted = ext4_encrypted_inode, | ||
| 1247 | }; | ||
| 1248 | #endif | 1247 | #endif |
| 1249 | 1248 | ||
| 1250 | #ifdef CONFIG_QUOTA | 1249 | #ifdef CONFIG_QUOTA |
| @@ -3996,7 +3995,9 @@ static int ext4_fill_super(struct super_block *sb, void *data, int silent) | |||
| 3996 | sb->s_op = &ext4_sops; | 3995 | sb->s_op = &ext4_sops; |
| 3997 | sb->s_export_op = &ext4_export_ops; | 3996 | sb->s_export_op = &ext4_export_ops; |
| 3998 | sb->s_xattr = ext4_xattr_handlers; | 3997 | sb->s_xattr = ext4_xattr_handlers; |
| 3998 | #ifdef CONFIG_EXT4_FS_ENCRYPTION | ||
| 3999 | sb->s_cop = &ext4_cryptops; | 3999 | sb->s_cop = &ext4_cryptops; |
| 4000 | #endif | ||
| 4000 | #ifdef CONFIG_QUOTA | 4001 | #ifdef CONFIG_QUOTA |
| 4001 | sb->dq_op = &ext4_quota_operations; | 4002 | sb->dq_op = &ext4_quota_operations; |
| 4002 | if (ext4_has_feature_quota(sb)) | 4003 | if (ext4_has_feature_quota(sb)) |
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 4b4a72f392be..115204fdefcc 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h | |||
| @@ -23,13 +23,11 @@ | |||
| 23 | #include <linux/bio.h> | 23 | #include <linux/bio.h> |
| 24 | #include <linux/blkdev.h> | 24 | #include <linux/blkdev.h> |
| 25 | #include <linux/quotaops.h> | 25 | #include <linux/quotaops.h> |
| 26 | #ifdef CONFIG_F2FS_FS_ENCRYPTION | ||
| 27 | #include <linux/fscrypt_supp.h> | ||
| 28 | #else | ||
| 29 | #include <linux/fscrypt_notsupp.h> | ||
| 30 | #endif | ||
| 31 | #include <crypto/hash.h> | 26 | #include <crypto/hash.h> |
| 32 | 27 | ||
| 28 | #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION) | ||
| 29 | #include <linux/fscrypt.h> | ||
| 30 | |||
| 33 | #ifdef CONFIG_F2FS_CHECK_FS | 31 | #ifdef CONFIG_F2FS_CHECK_FS |
| 34 | #define f2fs_bug_on(sbi, condition) BUG_ON(condition) | 32 | #define f2fs_bug_on(sbi, condition) BUG_ON(condition) |
| 35 | #else | 33 | #else |
| @@ -2949,6 +2947,7 @@ static inline void f2fs_set_encrypted_inode(struct inode *inode) | |||
| 2949 | { | 2947 | { |
| 2950 | #ifdef CONFIG_F2FS_FS_ENCRYPTION | 2948 | #ifdef CONFIG_F2FS_FS_ENCRYPTION |
| 2951 | file_set_encrypt(inode); | 2949 | file_set_encrypt(inode); |
| 2950 | inode->i_flags |= S_ENCRYPTED; | ||
| 2952 | #endif | 2951 | #endif |
| 2953 | } | 2952 | } |
| 2954 | 2953 | ||
diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c index 50c88e37ed66..53fb08810ee9 100644 --- a/fs/f2fs/inode.c +++ b/fs/f2fs/inode.c | |||
| @@ -43,8 +43,11 @@ void f2fs_set_inode_flags(struct inode *inode) | |||
| 43 | new_fl |= S_NOATIME; | 43 | new_fl |= S_NOATIME; |
| 44 | if (flags & FS_DIRSYNC_FL) | 44 | if (flags & FS_DIRSYNC_FL) |
| 45 | new_fl |= S_DIRSYNC; | 45 | new_fl |= S_DIRSYNC; |
| 46 | if (f2fs_encrypted_inode(inode)) | ||
| 47 | new_fl |= S_ENCRYPTED; | ||
| 46 | inode_set_flags(inode, new_fl, | 48 | inode_set_flags(inode, new_fl, |
| 47 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | 49 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC| |
| 50 | S_ENCRYPTED); | ||
| 48 | } | 51 | } |
| 49 | 52 | ||
| 50 | static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri) | 53 | static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri) |
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c index 933c3d529e65..97e03c637e90 100644 --- a/fs/f2fs/super.c +++ b/fs/f2fs/super.c | |||
| @@ -1594,14 +1594,9 @@ static const struct fscrypt_operations f2fs_cryptops = { | |||
| 1594 | .key_prefix = "f2fs:", | 1594 | .key_prefix = "f2fs:", |
| 1595 | .get_context = f2fs_get_context, | 1595 | .get_context = f2fs_get_context, |
| 1596 | .set_context = f2fs_set_context, | 1596 | .set_context = f2fs_set_context, |
| 1597 | .is_encrypted = f2fs_encrypted_inode, | ||
| 1598 | .empty_dir = f2fs_empty_dir, | 1597 | .empty_dir = f2fs_empty_dir, |
| 1599 | .max_namelen = f2fs_max_namelen, | 1598 | .max_namelen = f2fs_max_namelen, |
| 1600 | }; | 1599 | }; |
| 1601 | #else | ||
| 1602 | static const struct fscrypt_operations f2fs_cryptops = { | ||
| 1603 | .is_encrypted = f2fs_encrypted_inode, | ||
| 1604 | }; | ||
| 1605 | #endif | 1600 | #endif |
| 1606 | 1601 | ||
| 1607 | static struct inode *f2fs_nfs_get_inode(struct super_block *sb, | 1602 | static struct inode *f2fs_nfs_get_inode(struct super_block *sb, |
| @@ -2320,7 +2315,9 @@ try_onemore: | |||
| 2320 | #endif | 2315 | #endif |
| 2321 | 2316 | ||
| 2322 | sb->s_op = &f2fs_sops; | 2317 | sb->s_op = &f2fs_sops; |
| 2318 | #ifdef CONFIG_F2FS_FS_ENCRYPTION | ||
| 2323 | sb->s_cop = &f2fs_cryptops; | 2319 | sb->s_cop = &f2fs_cryptops; |
| 2320 | #endif | ||
| 2324 | sb->s_xattr = f2fs_xattr_handlers; | 2321 | sb->s_xattr = f2fs_xattr_handlers; |
| 2325 | sb->s_export_op = &f2fs_export_ops; | 2322 | sb->s_export_op = &f2fs_export_ops; |
| 2326 | sb->s_magic = F2FS_SUPER_MAGIC; | 2323 | sb->s_magic = F2FS_SUPER_MAGIC; |
diff --git a/fs/ubifs/crypto.c b/fs/ubifs/crypto.c index 16a5d5c82073..616a688f5d8f 100644 --- a/fs/ubifs/crypto.c +++ b/fs/ubifs/crypto.c | |||
| @@ -88,7 +88,6 @@ const struct fscrypt_operations ubifs_crypt_operations = { | |||
| 88 | .key_prefix = "ubifs:", | 88 | .key_prefix = "ubifs:", |
| 89 | .get_context = ubifs_crypt_get_context, | 89 | .get_context = ubifs_crypt_get_context, |
| 90 | .set_context = ubifs_crypt_set_context, | 90 | .set_context = ubifs_crypt_set_context, |
| 91 | .is_encrypted = __ubifs_crypt_is_encrypted, | ||
| 92 | .empty_dir = ubifs_crypt_empty_dir, | 91 | .empty_dir = ubifs_crypt_empty_dir, |
| 93 | .max_namelen = ubifs_crypt_max_namelen, | 92 | .max_namelen = ubifs_crypt_max_namelen, |
| 94 | }; | 93 | }; |
diff --git a/fs/ubifs/ioctl.c b/fs/ubifs/ioctl.c index fdc311246807..0164bcc827f8 100644 --- a/fs/ubifs/ioctl.c +++ b/fs/ubifs/ioctl.c | |||
| @@ -38,7 +38,8 @@ void ubifs_set_inode_flags(struct inode *inode) | |||
| 38 | { | 38 | { |
| 39 | unsigned int flags = ubifs_inode(inode)->flags; | 39 | unsigned int flags = ubifs_inode(inode)->flags; |
| 40 | 40 | ||
| 41 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC); | 41 | inode->i_flags &= ~(S_SYNC | S_APPEND | S_IMMUTABLE | S_DIRSYNC | |
| 42 | S_ENCRYPTED); | ||
| 42 | if (flags & UBIFS_SYNC_FL) | 43 | if (flags & UBIFS_SYNC_FL) |
| 43 | inode->i_flags |= S_SYNC; | 44 | inode->i_flags |= S_SYNC; |
| 44 | if (flags & UBIFS_APPEND_FL) | 45 | if (flags & UBIFS_APPEND_FL) |
| @@ -47,6 +48,8 @@ void ubifs_set_inode_flags(struct inode *inode) | |||
| 47 | inode->i_flags |= S_IMMUTABLE; | 48 | inode->i_flags |= S_IMMUTABLE; |
| 48 | if (flags & UBIFS_DIRSYNC_FL) | 49 | if (flags & UBIFS_DIRSYNC_FL) |
| 49 | inode->i_flags |= S_DIRSYNC; | 50 | inode->i_flags |= S_DIRSYNC; |
| 51 | if (flags & UBIFS_CRYPT_FL) | ||
| 52 | inode->i_flags |= S_ENCRYPTED; | ||
| 50 | } | 53 | } |
| 51 | 54 | ||
| 52 | /* | 55 | /* |
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c index 5496b17b959c..7503e7cdf870 100644 --- a/fs/ubifs/super.c +++ b/fs/ubifs/super.c | |||
| @@ -2007,12 +2007,6 @@ static struct ubifs_info *alloc_ubifs_info(struct ubi_volume_desc *ubi) | |||
| 2007 | return c; | 2007 | return c; |
| 2008 | } | 2008 | } |
| 2009 | 2009 | ||
| 2010 | #ifndef CONFIG_UBIFS_FS_ENCRYPTION | ||
| 2011 | const struct fscrypt_operations ubifs_crypt_operations = { | ||
| 2012 | .is_encrypted = __ubifs_crypt_is_encrypted, | ||
| 2013 | }; | ||
| 2014 | #endif | ||
| 2015 | |||
| 2016 | static int ubifs_fill_super(struct super_block *sb, void *data, int silent) | 2010 | static int ubifs_fill_super(struct super_block *sb, void *data, int silent) |
| 2017 | { | 2011 | { |
| 2018 | struct ubifs_info *c = sb->s_fs_info; | 2012 | struct ubifs_info *c = sb->s_fs_info; |
| @@ -2055,7 +2049,9 @@ static int ubifs_fill_super(struct super_block *sb, void *data, int silent) | |||
| 2055 | sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE; | 2049 | sb->s_maxbytes = c->max_inode_sz = MAX_LFS_FILESIZE; |
| 2056 | sb->s_op = &ubifs_super_operations; | 2050 | sb->s_op = &ubifs_super_operations; |
| 2057 | sb->s_xattr = ubifs_xattr_handlers; | 2051 | sb->s_xattr = ubifs_xattr_handlers; |
| 2052 | #ifdef CONFIG_UBIFS_FS_ENCRYPTION | ||
| 2058 | sb->s_cop = &ubifs_crypt_operations; | 2053 | sb->s_cop = &ubifs_crypt_operations; |
| 2054 | #endif | ||
| 2059 | 2055 | ||
| 2060 | mutex_lock(&c->umount_mutex); | 2056 | mutex_lock(&c->umount_mutex); |
| 2061 | err = mount_ubifs(c); | 2057 | err = mount_ubifs(c); |
diff --git a/fs/ubifs/ubifs.h b/fs/ubifs/ubifs.h index cd43651f1731..63c7468147eb 100644 --- a/fs/ubifs/ubifs.h +++ b/fs/ubifs/ubifs.h | |||
| @@ -38,12 +38,11 @@ | |||
| 38 | #include <linux/backing-dev.h> | 38 | #include <linux/backing-dev.h> |
| 39 | #include <linux/security.h> | 39 | #include <linux/security.h> |
| 40 | #include <linux/xattr.h> | 40 | #include <linux/xattr.h> |
| 41 | #ifdef CONFIG_UBIFS_FS_ENCRYPTION | ||
| 42 | #include <linux/fscrypt_supp.h> | ||
| 43 | #else | ||
| 44 | #include <linux/fscrypt_notsupp.h> | ||
| 45 | #endif | ||
| 46 | #include <linux/random.h> | 41 | #include <linux/random.h> |
| 42 | |||
| 43 | #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_UBIFS_FS_ENCRYPTION) | ||
| 44 | #include <linux/fscrypt.h> | ||
| 45 | |||
| 47 | #include "ubifs-media.h" | 46 | #include "ubifs-media.h" |
| 48 | 47 | ||
| 49 | /* Version of this UBIFS implementation */ | 48 | /* Version of this UBIFS implementation */ |
| @@ -1835,18 +1834,13 @@ int ubifs_decrypt(const struct inode *inode, struct ubifs_data_node *dn, | |||
| 1835 | 1834 | ||
| 1836 | extern const struct fscrypt_operations ubifs_crypt_operations; | 1835 | extern const struct fscrypt_operations ubifs_crypt_operations; |
| 1837 | 1836 | ||
| 1838 | static inline bool __ubifs_crypt_is_encrypted(struct inode *inode) | 1837 | static inline bool ubifs_crypt_is_encrypted(const struct inode *inode) |
| 1839 | { | 1838 | { |
| 1840 | struct ubifs_inode *ui = ubifs_inode(inode); | 1839 | const struct ubifs_inode *ui = ubifs_inode(inode); |
| 1841 | 1840 | ||
| 1842 | return ui->flags & UBIFS_CRYPT_FL; | 1841 | return ui->flags & UBIFS_CRYPT_FL; |
| 1843 | } | 1842 | } |
| 1844 | 1843 | ||
| 1845 | static inline bool ubifs_crypt_is_encrypted(const struct inode *inode) | ||
| 1846 | { | ||
| 1847 | return __ubifs_crypt_is_encrypted((struct inode *)inode); | ||
| 1848 | } | ||
| 1849 | |||
| 1850 | /* Normal UBIFS messages */ | 1844 | /* Normal UBIFS messages */ |
| 1851 | __printf(2, 3) | 1845 | __printf(2, 3) |
| 1852 | void ubifs_msg(const struct ubifs_info *c, const char *fmt, ...); | 1846 | void ubifs_msg(const struct ubifs_info *c, const char *fmt, ...); |
diff --git a/fs/ubifs/xattr.c b/fs/ubifs/xattr.c index c13eae819cbc..5ddc89d564fd 100644 --- a/fs/ubifs/xattr.c +++ b/fs/ubifs/xattr.c | |||
| @@ -170,6 +170,7 @@ static int create_xattr(struct ubifs_info *c, struct inode *host, | |||
| 170 | err = ubifs_jnl_update(c, host, nm, inode, 0, 1); | 170 | err = ubifs_jnl_update(c, host, nm, inode, 0, 1); |
| 171 | if (err) | 171 | if (err) |
| 172 | goto out_cancel; | 172 | goto out_cancel; |
| 173 | ubifs_set_inode_flags(host); | ||
| 173 | mutex_unlock(&host_ui->ui_mutex); | 174 | mutex_unlock(&host_ui->ui_mutex); |
| 174 | 175 | ||
| 175 | ubifs_release_budget(c, &req); | 176 | ubifs_release_budget(c, &req); |
diff --git a/include/linux/fs.h b/include/linux/fs.h index e1f75a3b4af5..269086440071 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h | |||
| @@ -1854,6 +1854,7 @@ struct super_operations { | |||
| 1854 | #else | 1854 | #else |
| 1855 | #define S_DAX 0 /* Make all the DAX code disappear */ | 1855 | #define S_DAX 0 /* Make all the DAX code disappear */ |
| 1856 | #endif | 1856 | #endif |
| 1857 | #define S_ENCRYPTED 16384 /* Encrypted file (using fs/crypto/) */ | ||
| 1857 | 1858 | ||
| 1858 | /* | 1859 | /* |
| 1859 | * Note that nosuid etc flags are inode-specific: setting some file-system | 1860 | * Note that nosuid etc flags are inode-specific: setting some file-system |
| @@ -1893,6 +1894,7 @@ static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags | |||
| 1893 | #define IS_AUTOMOUNT(inode) ((inode)->i_flags & S_AUTOMOUNT) | 1894 | #define IS_AUTOMOUNT(inode) ((inode)->i_flags & S_AUTOMOUNT) |
| 1894 | #define IS_NOSEC(inode) ((inode)->i_flags & S_NOSEC) | 1895 | #define IS_NOSEC(inode) ((inode)->i_flags & S_NOSEC) |
| 1895 | #define IS_DAX(inode) ((inode)->i_flags & S_DAX) | 1896 | #define IS_DAX(inode) ((inode)->i_flags & S_DAX) |
| 1897 | #define IS_ENCRYPTED(inode) ((inode)->i_flags & S_ENCRYPTED) | ||
| 1896 | 1898 | ||
| 1897 | #define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \ | 1899 | #define IS_WHITEOUT(inode) (S_ISCHR(inode->i_mode) && \ |
| 1898 | (inode)->i_rdev == WHITEOUT_DEV) | 1900 | (inode)->i_rdev == WHITEOUT_DEV) |
diff --git a/include/linux/fscrypt.h b/include/linux/fscrypt.h new file mode 100644 index 000000000000..08b4b40c5aa8 --- /dev/null +++ b/include/linux/fscrypt.h | |||
| @@ -0,0 +1,294 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | ||
| 2 | /* | ||
| 3 | * fscrypt.h: declarations for per-file encryption | ||
| 4 | * | ||
| 5 | * Filesystems that implement per-file encryption include this header | ||
| 6 | * file with the __FS_HAS_ENCRYPTION set according to whether that filesystem | ||
| 7 | * is being built with encryption support or not. | ||
| 8 | * | ||
| 9 | * Copyright (C) 2015, Google, Inc. | ||
| 10 | * | ||
| 11 | * Written by Michael Halcrow, 2015. | ||
| 12 | * Modified by Jaegeuk Kim, 2015. | ||
| 13 | */ | ||
| 14 | #ifndef _LINUX_FSCRYPT_H | ||
| 15 | #define _LINUX_FSCRYPT_H | ||
| 16 | |||
| 17 | #include <linux/key.h> | ||
| 18 | #include <linux/fs.h> | ||
| 19 | #include <linux/mm.h> | ||
| 20 | #include <linux/bio.h> | ||
| 21 | #include <linux/dcache.h> | ||
| 22 | #include <crypto/skcipher.h> | ||
| 23 | #include <uapi/linux/fs.h> | ||
| 24 | |||
| 25 | #define FS_CRYPTO_BLOCK_SIZE 16 | ||
| 26 | |||
| 27 | struct fscrypt_info; | ||
| 28 | |||
| 29 | struct fscrypt_ctx { | ||
| 30 | union { | ||
| 31 | struct { | ||
| 32 | struct page *bounce_page; /* Ciphertext page */ | ||
| 33 | struct page *control_page; /* Original page */ | ||
| 34 | } w; | ||
| 35 | struct { | ||
| 36 | struct bio *bio; | ||
| 37 | struct work_struct work; | ||
| 38 | } r; | ||
| 39 | struct list_head free_list; /* Free list */ | ||
| 40 | }; | ||
| 41 | u8 flags; /* Flags */ | ||
| 42 | }; | ||
| 43 | |||
| 44 | /** | ||
| 45 | * For encrypted symlinks, the ciphertext length is stored at the beginning | ||
| 46 | * of the string in little-endian format. | ||
| 47 | */ | ||
| 48 | struct fscrypt_symlink_data { | ||
| 49 | __le16 len; | ||
| 50 | char encrypted_path[1]; | ||
| 51 | } __packed; | ||
| 52 | |||
| 53 | struct fscrypt_str { | ||
| 54 | unsigned char *name; | ||
| 55 | u32 len; | ||
| 56 | }; | ||
| 57 | |||
| 58 | struct fscrypt_name { | ||
| 59 | const struct qstr *usr_fname; | ||
| 60 | struct fscrypt_str disk_name; | ||
| 61 | u32 hash; | ||
| 62 | u32 minor_hash; | ||
| 63 | struct fscrypt_str crypto_buf; | ||
| 64 | }; | ||
| 65 | |||
| 66 | #define FSTR_INIT(n, l) { .name = n, .len = l } | ||
| 67 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) | ||
| 68 | #define fname_name(p) ((p)->disk_name.name) | ||
| 69 | #define fname_len(p) ((p)->disk_name.len) | ||
| 70 | |||
| 71 | /* | ||
| 72 | * fscrypt superblock flags | ||
| 73 | */ | ||
| 74 | #define FS_CFLG_OWN_PAGES (1U << 1) | ||
| 75 | |||
| 76 | /* | ||
| 77 | * crypto opertions for filesystems | ||
| 78 | */ | ||
| 79 | struct fscrypt_operations { | ||
| 80 | unsigned int flags; | ||
| 81 | const char *key_prefix; | ||
| 82 | int (*get_context)(struct inode *, void *, size_t); | ||
| 83 | int (*set_context)(struct inode *, const void *, size_t, void *); | ||
| 84 | bool (*dummy_context)(struct inode *); | ||
| 85 | bool (*empty_dir)(struct inode *); | ||
| 86 | unsigned (*max_namelen)(struct inode *); | ||
| 87 | }; | ||
| 88 | |||
| 89 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ | ||
| 90 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 | ||
| 91 | |||
| 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 | |||
| 100 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, | ||
| 101 | u32 filenames_mode) | ||
| 102 | { | ||
| 103 | if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC && | ||
| 104 | filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS) | ||
| 105 | return true; | ||
| 106 | |||
| 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; | ||
| 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 | |||
| 125 | #if __FS_HAS_ENCRYPTION | ||
| 126 | |||
| 127 | static inline struct page *fscrypt_control_page(struct page *page) | ||
| 128 | { | ||
| 129 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; | ||
| 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 | { | ||
| 143 | WARN_ON_ONCE(1); | ||
| 144 | return ERR_PTR(-EINVAL); | ||
| 145 | } | ||
| 146 | |||
| 147 | static inline bool fscrypt_has_encryption_key(const struct inode *inode) | ||
| 148 | { | ||
| 149 | return 0; | ||
| 150 | } | ||
| 151 | |||
| 152 | #include <linux/fscrypt_notsupp.h> | ||
| 153 | #endif /* __FS_HAS_ENCRYPTION */ | ||
| 154 | |||
| 155 | /** | ||
| 156 | * fscrypt_require_key - require an inode's encryption key | ||
| 157 | * @inode: the inode we need the key for | ||
| 158 | * | ||
| 159 | * If the inode is encrypted, set up its encryption key if not already done. | ||
| 160 | * Then require that the key be present and return -ENOKEY otherwise. | ||
| 161 | * | ||
| 162 | * No locks are needed, and the key will live as long as the struct inode --- so | ||
| 163 | * it won't go away from under you. | ||
| 164 | * | ||
| 165 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code | ||
| 166 | * if a problem occurred while setting up the encryption key. | ||
| 167 | */ | ||
| 168 | static inline int fscrypt_require_key(struct inode *inode) | ||
| 169 | { | ||
| 170 | if (IS_ENCRYPTED(inode)) { | ||
| 171 | int err = fscrypt_get_encryption_info(inode); | ||
| 172 | |||
| 173 | if (err) | ||
| 174 | return err; | ||
| 175 | if (!fscrypt_has_encryption_key(inode)) | ||
| 176 | return -ENOKEY; | ||
| 177 | } | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | /** | ||
| 182 | * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory | ||
| 183 | * @old_dentry: an existing dentry for the inode being linked | ||
| 184 | * @dir: the target directory | ||
| 185 | * @dentry: negative dentry for the target filename | ||
| 186 | * | ||
| 187 | * A new link can only be added to an encrypted directory if the directory's | ||
| 188 | * encryption key is available --- since otherwise we'd have no way to encrypt | ||
| 189 | * the filename. Therefore, we first set up the directory's encryption key (if | ||
| 190 | * not already done) and return an error if it's unavailable. | ||
| 191 | * | ||
| 192 | * We also verify that the link will not violate the constraint that all files | ||
| 193 | * in an encrypted directory tree use the same encryption policy. | ||
| 194 | * | ||
| 195 | * Return: 0 on success, -ENOKEY if the directory's encryption key is missing, | ||
| 196 | * -EPERM if the link would result in an inconsistent encryption policy, or | ||
| 197 | * another -errno code. | ||
| 198 | */ | ||
| 199 | static inline int fscrypt_prepare_link(struct dentry *old_dentry, | ||
| 200 | struct inode *dir, | ||
| 201 | struct dentry *dentry) | ||
| 202 | { | ||
| 203 | if (IS_ENCRYPTED(dir)) | ||
| 204 | return __fscrypt_prepare_link(d_inode(old_dentry), dir); | ||
| 205 | return 0; | ||
| 206 | } | ||
| 207 | |||
| 208 | /** | ||
| 209 | * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories | ||
| 210 | * @old_dir: source directory | ||
| 211 | * @old_dentry: dentry for source file | ||
| 212 | * @new_dir: target directory | ||
| 213 | * @new_dentry: dentry for target location (may be negative unless exchanging) | ||
| 214 | * @flags: rename flags (we care at least about %RENAME_EXCHANGE) | ||
| 215 | * | ||
| 216 | * Prepare for ->rename() where the source and/or target directories may be | ||
| 217 | * encrypted. A new link can only be added to an encrypted directory if the | ||
| 218 | * directory's encryption key is available --- since otherwise we'd have no way | ||
| 219 | * to encrypt the filename. A rename to an existing name, on the other hand, | ||
| 220 | * *is* cryptographically possible without the key. However, we take the more | ||
| 221 | * conservative approach and just forbid all no-key renames. | ||
| 222 | * | ||
| 223 | * We also verify that the rename will not violate the constraint that all files | ||
| 224 | * in an encrypted directory tree use the same encryption policy. | ||
| 225 | * | ||
| 226 | * Return: 0 on success, -ENOKEY if an encryption key is missing, -EPERM if the | ||
| 227 | * rename would cause inconsistent encryption policies, or another -errno code. | ||
| 228 | */ | ||
| 229 | static inline int fscrypt_prepare_rename(struct inode *old_dir, | ||
| 230 | struct dentry *old_dentry, | ||
| 231 | struct inode *new_dir, | ||
| 232 | struct dentry *new_dentry, | ||
| 233 | unsigned int flags) | ||
| 234 | { | ||
| 235 | if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir)) | ||
| 236 | return __fscrypt_prepare_rename(old_dir, old_dentry, | ||
| 237 | new_dir, new_dentry, flags); | ||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | /** | ||
| 242 | * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory | ||
| 243 | * @dir: directory being searched | ||
| 244 | * @dentry: filename being looked up | ||
| 245 | * @flags: lookup flags | ||
| 246 | * | ||
| 247 | * Prepare for ->lookup() in a directory which may be encrypted. Lookups can be | ||
| 248 | * done with or without the directory's encryption key; without the key, | ||
| 249 | * filenames are presented in encrypted form. Therefore, we'll try to set up | ||
| 250 | * the directory's encryption key, but even without it the lookup can continue. | ||
| 251 | * | ||
| 252 | * To allow invalidating stale dentries if the directory's encryption key is | ||
| 253 | * added later, we also install a custom ->d_revalidate() method and use the | ||
| 254 | * DCACHE_ENCRYPTED_WITH_KEY flag to indicate whether a given dentry is a | ||
| 255 | * plaintext name (flag set) or a ciphertext name (flag cleared). | ||
| 256 | * | ||
| 257 | * Return: 0 on success, -errno if a problem occurred while setting up the | ||
| 258 | * encryption key | ||
| 259 | */ | ||
| 260 | static inline int fscrypt_prepare_lookup(struct inode *dir, | ||
| 261 | struct dentry *dentry, | ||
| 262 | unsigned int flags) | ||
| 263 | { | ||
| 264 | if (IS_ENCRYPTED(dir)) | ||
| 265 | return __fscrypt_prepare_lookup(dir, dentry); | ||
| 266 | return 0; | ||
| 267 | } | ||
| 268 | |||
| 269 | /** | ||
| 270 | * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes | ||
| 271 | * @dentry: dentry through which the inode is being changed | ||
| 272 | * @attr: attributes to change | ||
| 273 | * | ||
| 274 | * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file, | ||
| 275 | * most attribute changes are allowed even without the encryption key. However, | ||
| 276 | * without the encryption key we do have to forbid truncates. This is needed | ||
| 277 | * because the size being truncated to may not be a multiple of the filesystem | ||
| 278 | * block size, and in that case we'd have to decrypt the final block, zero the | ||
| 279 | * portion past i_size, and re-encrypt it. (We *could* allow truncating to a | ||
| 280 | * filesystem block boundary, but it's simpler to just forbid all truncates --- | ||
| 281 | * and we already forbid all other contents modifications without the key.) | ||
| 282 | * | ||
| 283 | * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code | ||
| 284 | * if a problem occurred while setting up the encryption key. | ||
| 285 | */ | ||
| 286 | static inline int fscrypt_prepare_setattr(struct dentry *dentry, | ||
| 287 | struct iattr *attr) | ||
| 288 | { | ||
| 289 | if (attr->ia_valid & ATTR_SIZE) | ||
| 290 | return fscrypt_require_key(d_inode(dentry)); | ||
| 291 | return 0; | ||
| 292 | } | ||
| 293 | |||
| 294 | #endif /* _LINUX_FSCRYPT_H */ | ||
diff --git a/include/linux/fscrypt_common.h b/include/linux/fscrypt_common.h deleted file mode 100644 index 854d724978fa..000000000000 --- a/include/linux/fscrypt_common.h +++ /dev/null | |||
| @@ -1,142 +0,0 @@ | |||
| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | ||
| 2 | /* | ||
| 3 | * fscrypt_common.h: common declarations for per-file encryption | ||
| 4 | * | ||
| 5 | * Copyright (C) 2015, Google, Inc. | ||
| 6 | * | ||
| 7 | * Written by Michael Halcrow, 2015. | ||
| 8 | * Modified by Jaegeuk Kim, 2015. | ||
| 9 | */ | ||
| 10 | |||
| 11 | #ifndef _LINUX_FSCRYPT_COMMON_H | ||
| 12 | #define _LINUX_FSCRYPT_COMMON_H | ||
| 13 | |||
| 14 | #include <linux/key.h> | ||
| 15 | #include <linux/fs.h> | ||
| 16 | #include <linux/mm.h> | ||
| 17 | #include <linux/bio.h> | ||
| 18 | #include <linux/dcache.h> | ||
| 19 | #include <crypto/skcipher.h> | ||
| 20 | #include <uapi/linux/fs.h> | ||
| 21 | |||
| 22 | #define FS_CRYPTO_BLOCK_SIZE 16 | ||
| 23 | |||
| 24 | struct fscrypt_info; | ||
| 25 | |||
| 26 | struct fscrypt_ctx { | ||
| 27 | union { | ||
| 28 | struct { | ||
| 29 | struct page *bounce_page; /* Ciphertext page */ | ||
| 30 | struct page *control_page; /* Original page */ | ||
| 31 | } w; | ||
| 32 | struct { | ||
| 33 | struct bio *bio; | ||
| 34 | struct work_struct work; | ||
| 35 | } r; | ||
| 36 | struct list_head free_list; /* Free list */ | ||
| 37 | }; | ||
| 38 | u8 flags; /* Flags */ | ||
| 39 | }; | ||
| 40 | |||
| 41 | /** | ||
| 42 | * For encrypted symlinks, the ciphertext length is stored at the beginning | ||
| 43 | * of the string in little-endian format. | ||
| 44 | */ | ||
| 45 | struct fscrypt_symlink_data { | ||
| 46 | __le16 len; | ||
| 47 | char encrypted_path[1]; | ||
| 48 | } __packed; | ||
| 49 | |||
| 50 | struct fscrypt_str { | ||
| 51 | unsigned char *name; | ||
| 52 | u32 len; | ||
| 53 | }; | ||
| 54 | |||
| 55 | struct fscrypt_name { | ||
| 56 | const struct qstr *usr_fname; | ||
| 57 | struct fscrypt_str disk_name; | ||
| 58 | u32 hash; | ||
| 59 | u32 minor_hash; | ||
| 60 | struct fscrypt_str crypto_buf; | ||
| 61 | }; | ||
| 62 | |||
| 63 | #define FSTR_INIT(n, l) { .name = n, .len = l } | ||
| 64 | #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len) | ||
| 65 | #define fname_name(p) ((p)->disk_name.name) | ||
| 66 | #define fname_len(p) ((p)->disk_name.len) | ||
| 67 | |||
| 68 | /* | ||
| 69 | * fscrypt superblock flags | ||
| 70 | */ | ||
| 71 | #define FS_CFLG_OWN_PAGES (1U << 1) | ||
| 72 | |||
| 73 | /* | ||
| 74 | * crypto opertions for filesystems | ||
| 75 | */ | ||
| 76 | struct fscrypt_operations { | ||
| 77 | unsigned int flags; | ||
| 78 | const char *key_prefix; | ||
| 79 | int (*get_context)(struct inode *, void *, size_t); | ||
| 80 | int (*set_context)(struct inode *, const void *, size_t, void *); | ||
| 81 | bool (*dummy_context)(struct inode *); | ||
| 82 | bool (*is_encrypted)(struct inode *); | ||
| 83 | bool (*empty_dir)(struct inode *); | ||
| 84 | unsigned (*max_namelen)(struct inode *); | ||
| 85 | }; | ||
| 86 | |||
| 87 | /* Maximum value for the third parameter of fscrypt_operations.set_context(). */ | ||
| 88 | #define FSCRYPT_SET_CONTEXT_MAX_SIZE 28 | ||
| 89 | |||
| 90 | static inline bool fscrypt_dummy_context_enabled(struct inode *inode) | ||
| 91 | { | ||
| 92 | if (inode->i_sb->s_cop->dummy_context && | ||
| 93 | inode->i_sb->s_cop->dummy_context(inode)) | ||
| 94 | return true; | ||
| 95 | return false; | ||
| 96 | } | ||
| 97 | |||
| 98 | static inline bool fscrypt_valid_enc_modes(u32 contents_mode, | ||
| 99 | u32 filenames_mode) | ||
| 100 | { | ||
| 101 | if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC && | ||
| 102 | filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS) | ||
| 103 | return true; | ||
| 104 | |||
| 105 | if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS && | ||
| 106 | filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS) | ||
| 107 | return true; | ||
| 108 | |||
| 109 | return false; | ||
| 110 | } | ||
| 111 | |||
| 112 | static inline bool fscrypt_is_dot_dotdot(const struct qstr *str) | ||
| 113 | { | ||
| 114 | if (str->len == 1 && str->name[0] == '.') | ||
| 115 | return true; | ||
| 116 | |||
| 117 | if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.') | ||
| 118 | return true; | ||
| 119 | |||
| 120 | return false; | ||
| 121 | } | ||
| 122 | |||
| 123 | static inline struct page *fscrypt_control_page(struct page *page) | ||
| 124 | { | ||
| 125 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) | ||
| 126 | return ((struct fscrypt_ctx *)page_private(page))->w.control_page; | ||
| 127 | #else | ||
| 128 | WARN_ON_ONCE(1); | ||
| 129 | return ERR_PTR(-EINVAL); | ||
| 130 | #endif | ||
| 131 | } | ||
| 132 | |||
| 133 | static inline int fscrypt_has_encryption_key(const struct inode *inode) | ||
| 134 | { | ||
| 135 | #if IS_ENABLED(CONFIG_FS_ENCRYPTION) | ||
| 136 | return (inode->i_crypt_info != NULL); | ||
| 137 | #else | ||
| 138 | return 0; | ||
| 139 | #endif | ||
| 140 | } | ||
| 141 | |||
| 142 | #endif /* _LINUX_FSCRYPT_COMMON_H */ | ||
diff --git a/include/linux/fscrypt_notsupp.h b/include/linux/fscrypt_notsupp.h index 19609ceea350..63e58808519a 100644 --- a/include/linux/fscrypt_notsupp.h +++ b/include/linux/fscrypt_notsupp.h | |||
| @@ -4,13 +4,16 @@ | |||
| 4 | * | 4 | * |
| 5 | * This stubs out the fscrypt functions for filesystems configured without | 5 | * This stubs out the fscrypt functions for filesystems configured without |
| 6 | * encryption support. | 6 | * encryption support. |
| 7 | * | ||
| 8 | * Do not include this file directly. Use fscrypt.h instead! | ||
| 7 | */ | 9 | */ |
| 10 | #ifndef _LINUX_FSCRYPT_H | ||
| 11 | #error "Incorrect include of linux/fscrypt_notsupp.h!" | ||
| 12 | #endif | ||
| 8 | 13 | ||
| 9 | #ifndef _LINUX_FSCRYPT_NOTSUPP_H | 14 | #ifndef _LINUX_FSCRYPT_NOTSUPP_H |
| 10 | #define _LINUX_FSCRYPT_NOTSUPP_H | 15 | #define _LINUX_FSCRYPT_NOTSUPP_H |
| 11 | 16 | ||
| 12 | #include <linux/fscrypt_common.h> | ||
| 13 | |||
| 14 | /* crypto.c */ | 17 | /* crypto.c */ |
| 15 | static inline struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode, | 18 | static inline struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *inode, |
| 16 | gfp_t gfp_flags) | 19 | gfp_t gfp_flags) |
| @@ -98,7 +101,7 @@ static inline int fscrypt_setup_filename(struct inode *dir, | |||
| 98 | const struct qstr *iname, | 101 | const struct qstr *iname, |
| 99 | int lookup, struct fscrypt_name *fname) | 102 | int lookup, struct fscrypt_name *fname) |
| 100 | { | 103 | { |
| 101 | if (dir->i_sb->s_cop->is_encrypted(dir)) | 104 | if (IS_ENCRYPTED(dir)) |
| 102 | return -EOPNOTSUPP; | 105 | return -EOPNOTSUPP; |
| 103 | 106 | ||
| 104 | memset(fname, 0, sizeof(struct fscrypt_name)); | 107 | memset(fname, 0, sizeof(struct fscrypt_name)); |
| @@ -175,4 +178,34 @@ static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk, | |||
| 175 | return -EOPNOTSUPP; | 178 | return -EOPNOTSUPP; |
| 176 | } | 179 | } |
| 177 | 180 | ||
| 181 | /* hooks.c */ | ||
| 182 | |||
| 183 | static inline int fscrypt_file_open(struct inode *inode, struct file *filp) | ||
| 184 | { | ||
| 185 | if (IS_ENCRYPTED(inode)) | ||
| 186 | return -EOPNOTSUPP; | ||
| 187 | return 0; | ||
| 188 | } | ||
| 189 | |||
| 190 | static inline int __fscrypt_prepare_link(struct inode *inode, | ||
| 191 | struct inode *dir) | ||
| 192 | { | ||
| 193 | return -EOPNOTSUPP; | ||
| 194 | } | ||
| 195 | |||
| 196 | static inline int __fscrypt_prepare_rename(struct inode *old_dir, | ||
| 197 | struct dentry *old_dentry, | ||
| 198 | struct inode *new_dir, | ||
| 199 | struct dentry *new_dentry, | ||
| 200 | unsigned int flags) | ||
| 201 | { | ||
| 202 | return -EOPNOTSUPP; | ||
| 203 | } | ||
| 204 | |||
| 205 | static inline int __fscrypt_prepare_lookup(struct inode *dir, | ||
| 206 | struct dentry *dentry) | ||
| 207 | { | ||
| 208 | return -EOPNOTSUPP; | ||
| 209 | } | ||
| 210 | |||
| 178 | #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ | 211 | #endif /* _LINUX_FSCRYPT_NOTSUPP_H */ |
diff --git a/include/linux/fscrypt_supp.h b/include/linux/fscrypt_supp.h index 5153dce22f09..cf9e9fc02f0a 100644 --- a/include/linux/fscrypt_supp.h +++ b/include/linux/fscrypt_supp.h | |||
| @@ -2,14 +2,15 @@ | |||
| 2 | /* | 2 | /* |
| 3 | * fscrypt_supp.h | 3 | * fscrypt_supp.h |
| 4 | * | 4 | * |
| 5 | * This is included by filesystems configured with encryption support. | 5 | * Do not include this file directly. Use fscrypt.h instead! |
| 6 | */ | 6 | */ |
| 7 | #ifndef _LINUX_FSCRYPT_H | ||
| 8 | #error "Incorrect include of linux/fscrypt_supp.h!" | ||
| 9 | #endif | ||
| 7 | 10 | ||
| 8 | #ifndef _LINUX_FSCRYPT_SUPP_H | 11 | #ifndef _LINUX_FSCRYPT_SUPP_H |
| 9 | #define _LINUX_FSCRYPT_SUPP_H | 12 | #define _LINUX_FSCRYPT_SUPP_H |
| 10 | 13 | ||
| 11 | #include <linux/fscrypt_common.h> | ||
| 12 | |||
| 13 | /* crypto.c */ | 14 | /* crypto.c */ |
| 14 | extern struct kmem_cache *fscrypt_info_cachep; | 15 | extern struct kmem_cache *fscrypt_info_cachep; |
| 15 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); | 16 | extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t); |
| @@ -143,4 +144,14 @@ extern void fscrypt_pullback_bio_page(struct page **, bool); | |||
| 143 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, | 144 | extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t, |
| 144 | unsigned int); | 145 | unsigned int); |
| 145 | 146 | ||
| 147 | /* hooks.c */ | ||
| 148 | extern int fscrypt_file_open(struct inode *inode, struct file *filp); | ||
| 149 | extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir); | ||
| 150 | extern int __fscrypt_prepare_rename(struct inode *old_dir, | ||
| 151 | struct dentry *old_dentry, | ||
| 152 | struct inode *new_dir, | ||
| 153 | struct dentry *new_dentry, | ||
| 154 | unsigned int flags); | ||
| 155 | extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry); | ||
| 156 | |||
| 146 | #endif /* _LINUX_FSCRYPT_SUPP_H */ | 157 | #endif /* _LINUX_FSCRYPT_SUPP_H */ |
