License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 14:07:57 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-04-26 07:12:50 +00:00
|
|
|
/*
|
2015-05-15 23:26:10 +00:00
|
|
|
* This contains functions for filename crypto management
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2015, Google, Inc.
|
|
|
|
* Copyright (C) 2015, Motorola Mobility
|
|
|
|
*
|
|
|
|
* Written by Uday Savagaonkar, 2014.
|
2015-05-15 23:26:10 +00:00
|
|
|
* Modified by Jaegeuk Kim, 2015.
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
|
|
|
* This has not yet undergone a rigorous security audit.
|
|
|
|
*/
|
2015-05-15 23:26:10 +00:00
|
|
|
|
2015-04-26 07:12:50 +00:00
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/ratelimit.h>
|
2018-01-05 18:45:00 +00:00
|
|
|
#include <crypto/skcipher.h>
|
2016-11-27 01:32:46 +00:00
|
|
|
#include "fscrypt_private.h"
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2018-01-05 18:44:59 +00:00
|
|
|
static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
|
|
|
|
{
|
|
|
|
if (str->len == 1 && str->name[0] == '.')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-26 07:12:50 +00:00
|
|
|
/**
|
2016-09-15 21:25:55 +00:00
|
|
|
* fname_encrypt() - encrypt a filename
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
2016-09-15 21:25:55 +00:00
|
|
|
* The caller must have allocated sufficient memory for the @oname string.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
2015-04-26 07:12:50 +00:00
|
|
|
*/
|
fscrypt: new helper functions for ->symlink()
Currently, filesystems supporting fscrypt need to implement some tricky
logic when creating encrypted symlinks, including handling a peculiar
on-disk format (struct fscrypt_symlink_data) and correctly calculating
the size of the encrypted symlink. Introduce helper functions to make
things a bit easier:
- fscrypt_prepare_symlink() computes and validates the size the symlink
target will require on-disk.
- fscrypt_encrypt_symlink() creates the encrypted target if needed.
The new helpers actually fix some subtle bugs. First, when checking
whether the symlink target was too long, filesystems didn't account for
the fact that the NUL padding is meant to be truncated if it would cause
the maximum length to be exceeded, as is done for filenames in
directories. Consequently users would receive ENAMETOOLONG when
creating symlinks close to what is supposed to be the maximum length.
For example, with EXT4 with a 4K block size, the maximum symlink target
length in an encrypted directory is supposed to be 4093 bytes (in
comparison to 4095 in an unencrypted directory), but in
FS_POLICY_FLAGS_PAD_32-mode only up to 4064 bytes were accepted.
Second, symlink targets of "." and ".." were not being encrypted, even
though they should be, as these names are special in *directory entries*
but not in symlink targets. Fortunately, we can fix this simply by
starting to encrypt them, as old kernels already accept them in
encrypted form.
Third, the output string length the filesystems were providing when
doing the actual encryption was incorrect, as it was forgotten to
exclude 'sizeof(struct fscrypt_symlink_data)'. Fortunately though, this
bug didn't make a difference.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2018-01-05 18:45:01 +00:00
|
|
|
int fname_encrypt(struct inode *inode,
|
|
|
|
const struct qstr *iname, struct fscrypt_str *oname)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
2016-01-24 13:17:49 +00:00
|
|
|
struct skcipher_request *req = NULL;
|
2017-10-18 07:00:44 +00:00
|
|
|
DECLARE_CRYPTO_WAIT(wait);
|
2015-05-15 23:26:10 +00:00
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-01-24 13:17:49 +00:00
|
|
|
struct crypto_skcipher *tfm = ci->ci_ctfm;
|
2015-04-26 07:12:50 +00:00
|
|
|
int res = 0;
|
2015-05-15 23:26:10 +00:00
|
|
|
char iv[FS_CRYPTO_BLOCK_SIZE];
|
2016-11-14 01:35:52 +00:00
|
|
|
struct scatterlist sg;
|
2015-05-15 23:26:10 +00:00
|
|
|
int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
|
2016-11-14 01:35:52 +00:00
|
|
|
unsigned int lim;
|
|
|
|
unsigned int cryptlen;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
lim = inode->i_sb->s_cop->max_namelen(inode);
|
2015-04-26 07:12:50 +00:00
|
|
|
if (iname->len <= 0 || iname->len > lim)
|
|
|
|
return -EIO;
|
|
|
|
|
2016-11-14 01:35:52 +00:00
|
|
|
/*
|
|
|
|
* Copy the filename to the output buffer for encrypting in-place and
|
|
|
|
* pad it with the needed number of NUL bytes.
|
|
|
|
*/
|
fscrypt: new helper functions for ->symlink()
Currently, filesystems supporting fscrypt need to implement some tricky
logic when creating encrypted symlinks, including handling a peculiar
on-disk format (struct fscrypt_symlink_data) and correctly calculating
the size of the encrypted symlink. Introduce helper functions to make
things a bit easier:
- fscrypt_prepare_symlink() computes and validates the size the symlink
target will require on-disk.
- fscrypt_encrypt_symlink() creates the encrypted target if needed.
The new helpers actually fix some subtle bugs. First, when checking
whether the symlink target was too long, filesystems didn't account for
the fact that the NUL padding is meant to be truncated if it would cause
the maximum length to be exceeded, as is done for filenames in
directories. Consequently users would receive ENAMETOOLONG when
creating symlinks close to what is supposed to be the maximum length.
For example, with EXT4 with a 4K block size, the maximum symlink target
length in an encrypted directory is supposed to be 4093 bytes (in
comparison to 4095 in an unencrypted directory), but in
FS_POLICY_FLAGS_PAD_32-mode only up to 4064 bytes were accepted.
Second, symlink targets of "." and ".." were not being encrypted, even
though they should be, as these names are special in *directory entries*
but not in symlink targets. Fortunately, we can fix this simply by
starting to encrypt them, as old kernels already accept them in
encrypted form.
Third, the output string length the filesystems were providing when
doing the actual encryption was incorrect, as it was forgotten to
exclude 'sizeof(struct fscrypt_symlink_data)'. Fortunately though, this
bug didn't make a difference.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2018-01-05 18:45:01 +00:00
|
|
|
if (WARN_ON(oname->len < iname->len))
|
|
|
|
return -ENOBUFS;
|
2016-11-14 01:35:52 +00:00
|
|
|
cryptlen = max_t(unsigned int, iname->len, FS_CRYPTO_BLOCK_SIZE);
|
|
|
|
cryptlen = round_up(cryptlen, padding);
|
fscrypt: new helper functions for ->symlink()
Currently, filesystems supporting fscrypt need to implement some tricky
logic when creating encrypted symlinks, including handling a peculiar
on-disk format (struct fscrypt_symlink_data) and correctly calculating
the size of the encrypted symlink. Introduce helper functions to make
things a bit easier:
- fscrypt_prepare_symlink() computes and validates the size the symlink
target will require on-disk.
- fscrypt_encrypt_symlink() creates the encrypted target if needed.
The new helpers actually fix some subtle bugs. First, when checking
whether the symlink target was too long, filesystems didn't account for
the fact that the NUL padding is meant to be truncated if it would cause
the maximum length to be exceeded, as is done for filenames in
directories. Consequently users would receive ENAMETOOLONG when
creating symlinks close to what is supposed to be the maximum length.
For example, with EXT4 with a 4K block size, the maximum symlink target
length in an encrypted directory is supposed to be 4093 bytes (in
comparison to 4095 in an unencrypted directory), but in
FS_POLICY_FLAGS_PAD_32-mode only up to 4064 bytes were accepted.
Second, symlink targets of "." and ".." were not being encrypted, even
though they should be, as these names are special in *directory entries*
but not in symlink targets. Fortunately, we can fix this simply by
starting to encrypt them, as old kernels already accept them in
encrypted form.
Third, the output string length the filesystems were providing when
doing the actual encryption was incorrect, as it was forgotten to
exclude 'sizeof(struct fscrypt_symlink_data)'. Fortunately though, this
bug didn't make a difference.
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
2018-01-05 18:45:01 +00:00
|
|
|
cryptlen = min3(cryptlen, lim, oname->len);
|
2016-11-14 01:35:52 +00:00
|
|
|
memcpy(oname->name, iname->name, iname->len);
|
|
|
|
memset(oname->name + iname->len, 0, cryptlen - iname->len);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2016-11-14 01:35:52 +00:00
|
|
|
/* Initialize the IV */
|
|
|
|
memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2016-11-14 01:35:52 +00:00
|
|
|
/* Set up the encryption request */
|
2016-01-24 13:17:49 +00:00
|
|
|
req = skcipher_request_alloc(tfm, GFP_NOFS);
|
2015-04-26 07:12:50 +00:00
|
|
|
if (!req) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
2016-11-14 01:35:52 +00:00
|
|
|
"%s: skcipher_request_alloc() failed\n", __func__);
|
2015-04-26 07:12:50 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-01-24 13:17:49 +00:00
|
|
|
skcipher_request_set_callback(req,
|
2015-04-26 07:12:50 +00:00
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
|
2017-10-18 07:00:44 +00:00
|
|
|
crypto_req_done, &wait);
|
2016-11-14 01:35:52 +00:00
|
|
|
sg_init_one(&sg, oname->name, cryptlen);
|
|
|
|
skcipher_request_set_crypt(req, &sg, &sg, cryptlen, iv);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2016-11-14 01:35:52 +00:00
|
|
|
/* Do the encryption */
|
2017-10-18 07:00:44 +00:00
|
|
|
res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
|
2016-01-24 13:17:49 +00:00
|
|
|
skcipher_request_free(req);
|
2016-09-15 21:25:55 +00:00
|
|
|
if (res < 0) {
|
2015-04-26 07:12:50 +00:00
|
|
|
printk_ratelimited(KERN_ERR
|
|
|
|
"%s: Error (error code %d)\n", __func__, res);
|
2016-09-15 21:25:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
|
2016-11-14 01:35:52 +00:00
|
|
|
oname->len = cryptlen;
|
2016-09-15 21:25:55 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 21:25:55 +00:00
|
|
|
/**
|
|
|
|
* fname_decrypt() - decrypt a filename
|
|
|
|
*
|
|
|
|
* The caller must have allocated sufficient memory for the @oname string.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
2015-04-26 07:12:50 +00:00
|
|
|
*/
|
2015-05-15 23:26:10 +00:00
|
|
|
static int fname_decrypt(struct inode *inode,
|
|
|
|
const struct fscrypt_str *iname,
|
|
|
|
struct fscrypt_str *oname)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
2016-01-24 13:17:49 +00:00
|
|
|
struct skcipher_request *req = NULL;
|
2017-10-18 07:00:44 +00:00
|
|
|
DECLARE_CRYPTO_WAIT(wait);
|
2015-04-26 07:12:50 +00:00
|
|
|
struct scatterlist src_sg, dst_sg;
|
2015-05-15 23:26:10 +00:00
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-01-24 13:17:49 +00:00
|
|
|
struct crypto_skcipher *tfm = ci->ci_ctfm;
|
2015-04-26 07:12:50 +00:00
|
|
|
int res = 0;
|
2015-05-15 23:26:10 +00:00
|
|
|
char iv[FS_CRYPTO_BLOCK_SIZE];
|
|
|
|
unsigned lim;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
lim = inode->i_sb->s_cop->max_namelen(inode);
|
2015-04-26 07:12:50 +00:00
|
|
|
if (iname->len <= 0 || iname->len > lim)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
/* Allocate request */
|
2016-01-24 13:17:49 +00:00
|
|
|
req = skcipher_request_alloc(tfm, GFP_NOFS);
|
2015-04-26 07:12:50 +00:00
|
|
|
if (!req) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
|
|
|
"%s: crypto_request_alloc() failed\n", __func__);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-01-24 13:17:49 +00:00
|
|
|
skcipher_request_set_callback(req,
|
2015-04-26 07:12:50 +00:00
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
|
2017-10-18 07:00:44 +00:00
|
|
|
crypto_req_done, &wait);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
/* Initialize IV */
|
2015-05-15 23:26:10 +00:00
|
|
|
memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
/* Create decryption request */
|
|
|
|
sg_init_one(&src_sg, iname->name, iname->len);
|
|
|
|
sg_init_one(&dst_sg, oname->name, oname->len);
|
2016-01-24 13:17:49 +00:00
|
|
|
skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
|
2017-10-18 07:00:44 +00:00
|
|
|
res = crypto_wait_req(crypto_skcipher_decrypt(req), &wait);
|
2016-01-24 13:17:49 +00:00
|
|
|
skcipher_request_free(req);
|
2015-04-26 07:12:50 +00:00
|
|
|
if (res < 0) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
2015-05-15 23:26:10 +00:00
|
|
|
"%s: Error (error code %d)\n", __func__, res);
|
2015-04-26 07:12:50 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
oname->len = strnlen(oname->name, iname->len);
|
2016-09-15 21:25:55 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char *lookup_table =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
|
|
|
|
|
2017-04-24 17:00:10 +00:00
|
|
|
#define BASE64_CHARS(nbytes) DIV_ROUND_UP((nbytes) * 4, 3)
|
|
|
|
|
2015-04-26 07:12:50 +00:00
|
|
|
/**
|
2015-05-15 23:26:10 +00:00
|
|
|
* digest_encode() -
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
|
|
|
* Encodes the input digest using characters from the set [a-zA-Z0-9_+].
|
|
|
|
* The encoded string is roughly 4/3 times the size of the input string.
|
|
|
|
*/
|
|
|
|
static int digest_encode(const char *src, int len, char *dst)
|
|
|
|
{
|
|
|
|
int i = 0, bits = 0, ac = 0;
|
|
|
|
char *cp = dst;
|
|
|
|
|
|
|
|
while (i < len) {
|
|
|
|
ac += (((unsigned char) src[i]) << bits);
|
|
|
|
bits += 8;
|
|
|
|
do {
|
|
|
|
*cp++ = lookup_table[ac & 0x3f];
|
|
|
|
ac >>= 6;
|
|
|
|
bits -= 6;
|
|
|
|
} while (bits >= 6);
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (bits)
|
|
|
|
*cp++ = lookup_table[ac & 0x3f];
|
|
|
|
return cp - dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int digest_decode(const char *src, int len, char *dst)
|
|
|
|
{
|
|
|
|
int i = 0, bits = 0, ac = 0;
|
|
|
|
const char *p;
|
|
|
|
char *cp = dst;
|
|
|
|
|
|
|
|
while (i < len) {
|
|
|
|
p = strchr(lookup_table, src[i]);
|
|
|
|
if (p == NULL || src[i] == 0)
|
|
|
|
return -2;
|
|
|
|
ac += (p - lookup_table) << bits;
|
|
|
|
bits += 6;
|
|
|
|
if (bits >= 8) {
|
|
|
|
*cp++ = ac & 0xff;
|
|
|
|
ac >>= 8;
|
|
|
|
bits -= 8;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
if (ac)
|
|
|
|
return -1;
|
|
|
|
return cp - dst;
|
|
|
|
}
|
|
|
|
|
2016-11-13 21:20:47 +00:00
|
|
|
u32 fscrypt_fname_encrypted_size(const struct inode *inode, u32 ilen)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
2016-02-15 09:54:26 +00:00
|
|
|
int padding = 32;
|
2015-05-15 23:26:10 +00:00
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-02-15 09:54:26 +00:00
|
|
|
|
|
|
|
if (ci)
|
2015-05-15 23:26:10 +00:00
|
|
|
padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
|
2016-09-30 05:46:18 +00:00
|
|
|
ilen = max(ilen, (u32)FS_CRYPTO_BLOCK_SIZE);
|
|
|
|
return round_up(ilen, padding);
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-15 23:26:10 +00:00
|
|
|
* fscrypt_fname_crypto_alloc_obuff() -
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
|
|
|
* Allocates an output buffer that is sufficient for the crypto operation
|
|
|
|
* specified by the context and the direction.
|
|
|
|
*/
|
2016-11-13 21:20:47 +00:00
|
|
|
int fscrypt_fname_alloc_buffer(const struct inode *inode,
|
2015-05-15 23:26:10 +00:00
|
|
|
u32 ilen, struct fscrypt_str *crypto_str)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
2017-04-24 17:00:10 +00:00
|
|
|
u32 olen = fscrypt_fname_encrypted_size(inode, ilen);
|
|
|
|
const u32 max_encoded_len =
|
|
|
|
max_t(u32, BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE),
|
|
|
|
1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)));
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
crypto_str->len = olen;
|
2017-04-24 17:00:10 +00:00
|
|
|
olen = max(olen, max_encoded_len);
|
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
/*
|
|
|
|
* Allocated buffer can hold one more character to null-terminate the
|
|
|
|
* string
|
|
|
|
*/
|
2015-04-26 07:12:50 +00:00
|
|
|
crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
|
|
|
|
if (!(crypto_str->name))
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-15 23:26:10 +00:00
|
|
|
* fscrypt_fname_crypto_free_buffer() -
|
2015-04-26 07:12:50 +00:00
|
|
|
*
|
|
|
|
* Frees the buffer allocated for crypto operation.
|
|
|
|
*/
|
2015-05-15 23:26:10 +00:00
|
|
|
void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
|
|
|
if (!crypto_str)
|
|
|
|
return;
|
|
|
|
kfree(crypto_str->name);
|
|
|
|
crypto_str->name = NULL;
|
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_free_buffer);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
|
|
|
/**
|
2015-05-15 23:26:10 +00:00
|
|
|
* fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
|
|
|
|
* space
|
2016-09-15 21:25:55 +00:00
|
|
|
*
|
|
|
|
* The caller must have allocated sufficient memory for the @oname string.
|
|
|
|
*
|
2017-04-24 17:00:10 +00:00
|
|
|
* If the key is available, we'll decrypt the disk name; otherwise, we'll encode
|
|
|
|
* it for presentation. Short names are directly base64-encoded, while long
|
|
|
|
* names are encoded in fscrypt_digested_name format.
|
|
|
|
*
|
2016-09-15 21:25:55 +00:00
|
|
|
* Return: 0 on success, -errno on failure
|
2015-04-26 07:12:50 +00:00
|
|
|
*/
|
2015-05-15 23:26:10 +00:00
|
|
|
int fscrypt_fname_disk_to_usr(struct inode *inode,
|
|
|
|
u32 hash, u32 minor_hash,
|
|
|
|
const struct fscrypt_str *iname,
|
|
|
|
struct fscrypt_str *oname)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
|
|
|
const struct qstr qname = FSTR_TO_QSTR(iname);
|
2017-04-24 17:00:10 +00:00
|
|
|
struct fscrypt_digested_name digested_name;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
if (fscrypt_is_dot_dotdot(&qname)) {
|
2015-04-26 07:12:50 +00:00
|
|
|
oname->name[0] = '.';
|
|
|
|
oname->name[iname->len - 1] = '.';
|
|
|
|
oname->len = iname->len;
|
2016-09-15 21:25:55 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
if (iname->len < FS_CRYPTO_BLOCK_SIZE)
|
2016-02-06 03:37:27 +00:00
|
|
|
return -EUCLEAN;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
if (inode->i_crypt_info)
|
|
|
|
return fname_decrypt(inode, iname, oname);
|
|
|
|
|
2017-04-24 17:00:10 +00:00
|
|
|
if (iname->len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE) {
|
2016-09-15 21:25:55 +00:00
|
|
|
oname->len = digest_encode(iname->name, iname->len,
|
|
|
|
oname->name);
|
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
|
|
|
if (hash) {
|
2017-04-24 17:00:10 +00:00
|
|
|
digested_name.hash = hash;
|
|
|
|
digested_name.minor_hash = minor_hash;
|
2015-05-15 23:26:10 +00:00
|
|
|
} else {
|
2017-04-24 17:00:10 +00:00
|
|
|
digested_name.hash = 0;
|
|
|
|
digested_name.minor_hash = 0;
|
2015-05-15 23:26:10 +00:00
|
|
|
}
|
2017-04-24 17:00:10 +00:00
|
|
|
memcpy(digested_name.digest,
|
|
|
|
FSCRYPT_FNAME_DIGEST(iname->name, iname->len),
|
|
|
|
FSCRYPT_FNAME_DIGEST_SIZE);
|
2015-04-26 07:12:50 +00:00
|
|
|
oname->name[0] = '_';
|
2017-04-24 17:00:10 +00:00
|
|
|
oname->len = 1 + digest_encode((const char *)&digested_name,
|
|
|
|
sizeof(digested_name), oname->name + 1);
|
2016-09-15 21:25:55 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2017-04-24 17:00:10 +00:00
|
|
|
/**
|
|
|
|
* fscrypt_setup_filename() - prepare to search a possibly encrypted directory
|
|
|
|
* @dir: the directory that will be searched
|
|
|
|
* @iname: the user-provided filename being searched for
|
|
|
|
* @lookup: 1 if we're allowed to proceed without the key because it's
|
|
|
|
* ->lookup() or we're finding the dir_entry for deletion; 0 if we cannot
|
|
|
|
* proceed without the key because we're going to create the dir_entry.
|
|
|
|
* @fname: the filename information to be filled in
|
|
|
|
*
|
|
|
|
* Given a user-provided filename @iname, this function sets @fname->disk_name
|
|
|
|
* to the name that would be stored in the on-disk directory entry, if possible.
|
|
|
|
* If the directory is unencrypted this is simply @iname. Else, if we have the
|
|
|
|
* directory's encryption key, then @iname is the plaintext, so we encrypt it to
|
|
|
|
* get the disk_name.
|
|
|
|
*
|
|
|
|
* Else, for keyless @lookup operations, @iname is the presented ciphertext, so
|
|
|
|
* we decode it to get either the ciphertext disk_name (for short names) or the
|
|
|
|
* fscrypt_digested_name (for long names). Non-@lookup operations will be
|
|
|
|
* impossible in this case, so we fail them with ENOKEY.
|
|
|
|
*
|
|
|
|
* If successful, fscrypt_free_filename() must be called later to clean up.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -errno on failure
|
|
|
|
*/
|
2015-05-15 23:26:10 +00:00
|
|
|
int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
|
|
|
|
int lookup, struct fscrypt_name *fname)
|
2015-04-26 07:12:50 +00:00
|
|
|
{
|
2017-04-24 17:00:10 +00:00
|
|
|
int ret;
|
|
|
|
int digested;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
memset(fname, 0, sizeof(struct fscrypt_name));
|
2015-04-26 07:12:50 +00:00
|
|
|
fname->usr_fname = iname;
|
|
|
|
|
2017-10-09 19:15:36 +00:00
|
|
|
if (!IS_ENCRYPTED(dir) || fscrypt_is_dot_dotdot(iname)) {
|
2015-04-26 07:12:50 +00:00
|
|
|
fname->disk_name.name = (unsigned char *)iname->name;
|
|
|
|
fname->disk_name.len = iname->len;
|
2015-05-13 10:20:54 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
fscrypt: remove broken support for detecting keyring key revocation
Filesystem encryption ostensibly supported revoking a keyring key that
had been used to "unlock" encrypted files, causing those files to become
"locked" again. This was, however, buggy for several reasons, the most
severe of which was that when key revocation happened to be detected for
an inode, its fscrypt_info was immediately freed, even while other
threads could be using it for encryption or decryption concurrently.
This could be exploited to crash the kernel or worse.
This patch fixes the use-after-free by removing the code which detects
the keyring key having been revoked, invalidated, or expired. Instead,
an encrypted inode that is "unlocked" now simply remains unlocked until
it is evicted from memory. Note that this is no worse than the case for
block device-level encryption, e.g. dm-crypt, and it still remains
possible for a privileged user to evict unused pages, inodes, and
dentries by running 'sync; echo 3 > /proc/sys/vm/drop_caches', or by
simply unmounting the filesystem. In fact, one of those actions was
already needed anyway for key revocation to work even somewhat sanely.
This change is not expected to break any applications.
In the future I'd like to implement a real API for fscrypt key
revocation that interacts sanely with ongoing filesystem operations ---
waiting for existing operations to complete and blocking new operations,
and invalidating and sanitizing key material and plaintext from the VFS
caches. But this is a hard problem, and for now this bug must be fixed.
This bug affected almost all versions of ext4, f2fs, and ubifs
encryption, and it was potentially reachable in any kernel configured
with encryption support (CONFIG_EXT4_ENCRYPTION=y,
CONFIG_EXT4_FS_ENCRYPTION=y, CONFIG_F2FS_FS_ENCRYPTION=y, or
CONFIG_UBIFS_FS_ENCRYPTION=y). Note that older kernels did not use the
shared fs/crypto/ code, but due to the potential security implications
of this bug, it may still be worthwhile to backport this fix to them.
Fixes: b7236e21d55f ("ext4 crypto: reorganize how we store keys in the inode")
Cc: stable@vger.kernel.org # v4.2+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Acked-by: Michael Halcrow <mhalcrow@google.com>
2017-02-21 23:07:11 +00:00
|
|
|
ret = fscrypt_get_encryption_info(dir);
|
2015-05-15 23:26:10 +00:00
|
|
|
if (ret && ret != -EOPNOTSUPP)
|
2015-04-26 07:12:50 +00:00
|
|
|
return ret;
|
2015-05-15 23:26:10 +00:00
|
|
|
|
|
|
|
if (dir->i_crypt_info) {
|
|
|
|
ret = fscrypt_fname_alloc_buffer(dir, iname->len,
|
|
|
|
&fname->crypto_buf);
|
2016-09-15 21:25:55 +00:00
|
|
|
if (ret)
|
2015-05-13 10:20:54 +00:00
|
|
|
return ret;
|
2015-05-15 23:26:10 +00:00
|
|
|
ret = fname_encrypt(dir, iname, &fname->crypto_buf);
|
2016-09-15 21:25:55 +00:00
|
|
|
if (ret)
|
2015-05-29 00:06:40 +00:00
|
|
|
goto errout;
|
2015-04-26 07:12:50 +00:00
|
|
|
fname->disk_name.name = fname->crypto_buf.name;
|
|
|
|
fname->disk_name.len = fname->crypto_buf.len;
|
2015-05-13 10:20:54 +00:00
|
|
|
return 0;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
2015-05-29 00:06:40 +00:00
|
|
|
if (!lookup)
|
2016-12-05 19:12:44 +00:00
|
|
|
return -ENOKEY;
|
2015-04-26 07:12:50 +00:00
|
|
|
|
2015-05-15 23:26:10 +00:00
|
|
|
/*
|
|
|
|
* We don't have the key and we are doing a lookup; decode the
|
2015-04-26 07:12:50 +00:00
|
|
|
* user-supplied name
|
|
|
|
*/
|
2017-04-24 17:00:10 +00:00
|
|
|
if (iname->name[0] == '_') {
|
|
|
|
if (iname->len !=
|
|
|
|
1 + BASE64_CHARS(sizeof(struct fscrypt_digested_name)))
|
|
|
|
return -ENOENT;
|
|
|
|
digested = 1;
|
|
|
|
} else {
|
|
|
|
if (iname->len >
|
|
|
|
BASE64_CHARS(FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE))
|
|
|
|
return -ENOENT;
|
|
|
|
digested = 0;
|
|
|
|
}
|
2015-05-29 00:06:40 +00:00
|
|
|
|
2017-04-24 17:00:10 +00:00
|
|
|
fname->crypto_buf.name =
|
|
|
|
kmalloc(max_t(size_t, FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE,
|
|
|
|
sizeof(struct fscrypt_digested_name)),
|
|
|
|
GFP_KERNEL);
|
2015-05-29 00:06:40 +00:00
|
|
|
if (fname->crypto_buf.name == NULL)
|
|
|
|
return -ENOMEM;
|
2015-05-15 23:26:10 +00:00
|
|
|
|
2017-04-24 17:00:10 +00:00
|
|
|
ret = digest_decode(iname->name + digested, iname->len - digested,
|
2015-04-26 07:12:50 +00:00
|
|
|
fname->crypto_buf.name);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -ENOENT;
|
2015-05-29 00:06:40 +00:00
|
|
|
goto errout;
|
2015-04-26 07:12:50 +00:00
|
|
|
}
|
|
|
|
fname->crypto_buf.len = ret;
|
2017-04-24 17:00:10 +00:00
|
|
|
if (digested) {
|
|
|
|
const struct fscrypt_digested_name *n =
|
|
|
|
(const void *)fname->crypto_buf.name;
|
|
|
|
fname->hash = n->hash;
|
|
|
|
fname->minor_hash = n->minor_hash;
|
2015-04-26 07:12:50 +00:00
|
|
|
} else {
|
|
|
|
fname->disk_name.name = fname->crypto_buf.name;
|
|
|
|
fname->disk_name.len = fname->crypto_buf.len;
|
|
|
|
}
|
2015-05-13 10:20:54 +00:00
|
|
|
return 0;
|
2015-05-15 23:26:10 +00:00
|
|
|
|
2015-05-29 00:06:40 +00:00
|
|
|
errout:
|
2015-05-15 23:26:10 +00:00
|
|
|
fscrypt_fname_free_buffer(&fname->crypto_buf);
|
2015-04-26 07:12:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-15 23:26:10 +00:00
|
|
|
EXPORT_SYMBOL(fscrypt_setup_filename);
|