2015-04-26 14:12:50 +07:00
|
|
|
/*
|
2015-05-16 06:26:10 +07:00
|
|
|
* This contains functions for filename crypto management
|
2015-04-26 14:12:50 +07:00
|
|
|
*
|
|
|
|
* Copyright (C) 2015, Google, Inc.
|
|
|
|
* Copyright (C) 2015, Motorola Mobility
|
|
|
|
*
|
|
|
|
* Written by Uday Savagaonkar, 2014.
|
2015-05-16 06:26:10 +07:00
|
|
|
* Modified by Jaegeuk Kim, 2015.
|
2015-04-26 14:12:50 +07:00
|
|
|
*
|
|
|
|
* This has not yet undergone a rigorous security audit.
|
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
|
2015-04-26 14:12:50 +07:00
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/ratelimit.h>
|
2015-05-16 06:26:10 +07:00
|
|
|
#include <linux/fscrypto.h>
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
static u32 size_round_up(size_t size, size_t blksize)
|
|
|
|
{
|
|
|
|
return ((size + blksize - 1) / blksize) * blksize;
|
|
|
|
}
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* dir_crypt_complete() -
|
2015-04-26 14:12:50 +07:00
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
static void dir_crypt_complete(struct crypto_async_request *req, int res)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
2015-05-16 06:26:10 +07:00
|
|
|
struct fscrypt_completion_result *ecr = req->data;
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
if (res == -EINPROGRESS)
|
|
|
|
return;
|
|
|
|
ecr->res = res;
|
|
|
|
complete(&ecr->completion);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* fname_encrypt() -
|
2015-04-26 14:12:50 +07:00
|
|
|
*
|
|
|
|
* This function encrypts the input filename, and returns the length of the
|
|
|
|
* ciphertext. Errors are returned as negative numbers. We trust the caller to
|
|
|
|
* allocate sufficient memory to oname string.
|
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
static int fname_encrypt(struct inode *inode,
|
|
|
|
const struct qstr *iname, struct fscrypt_str *oname)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
|
|
|
u32 ciphertext_len;
|
2016-01-24 20:17:49 +07:00
|
|
|
struct skcipher_request *req = NULL;
|
2015-05-16 06:26:10 +07:00
|
|
|
DECLARE_FS_COMPLETION_RESULT(ecr);
|
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-01-24 20:17:49 +07:00
|
|
|
struct crypto_skcipher *tfm = ci->ci_ctfm;
|
2015-04-26 14:12:50 +07:00
|
|
|
int res = 0;
|
2015-05-16 06:26:10 +07:00
|
|
|
char iv[FS_CRYPTO_BLOCK_SIZE];
|
2015-04-26 14:12:50 +07:00
|
|
|
struct scatterlist src_sg, dst_sg;
|
2015-05-16 06:26:10 +07:00
|
|
|
int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
|
2015-04-26 14:12:50 +07:00
|
|
|
char *workbuf, buf[32], *alloc_buf = NULL;
|
2015-05-16 06:26:10 +07:00
|
|
|
unsigned lim;
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
lim = inode->i_sb->s_cop->max_namelen(inode);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (iname->len <= 0 || iname->len > lim)
|
|
|
|
return -EIO;
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
ciphertext_len = (iname->len < FS_CRYPTO_BLOCK_SIZE) ?
|
|
|
|
FS_CRYPTO_BLOCK_SIZE : iname->len;
|
|
|
|
ciphertext_len = size_round_up(ciphertext_len, padding);
|
2015-04-26 14:12:50 +07:00
|
|
|
ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
|
|
|
|
|
|
|
|
if (ciphertext_len <= sizeof(buf)) {
|
|
|
|
workbuf = buf;
|
|
|
|
} else {
|
|
|
|
alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
|
|
|
|
if (!alloc_buf)
|
|
|
|
return -ENOMEM;
|
|
|
|
workbuf = alloc_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate request */
|
2016-01-24 20:17:49 +07:00
|
|
|
req = skcipher_request_alloc(tfm, GFP_NOFS);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (!req) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
|
|
|
"%s: crypto_request_alloc() failed\n", __func__);
|
|
|
|
kfree(alloc_buf);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-01-24 20:17:49 +07:00
|
|
|
skcipher_request_set_callback(req,
|
2015-04-26 14:12:50 +07:00
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
|
2015-05-16 06:26:10 +07:00
|
|
|
dir_crypt_complete, &ecr);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/* Copy the input */
|
|
|
|
memcpy(workbuf, iname->name, iname->len);
|
|
|
|
if (iname->len < ciphertext_len)
|
|
|
|
memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
|
|
|
|
|
|
|
|
/* Initialize IV */
|
2015-05-16 06:26:10 +07:00
|
|
|
memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/* Create encryption request */
|
|
|
|
sg_init_one(&src_sg, workbuf, ciphertext_len);
|
|
|
|
sg_init_one(&dst_sg, oname->name, ciphertext_len);
|
2016-01-24 20:17:49 +07:00
|
|
|
skcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
|
|
|
|
res = crypto_skcipher_encrypt(req);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (res == -EINPROGRESS || res == -EBUSY) {
|
|
|
|
wait_for_completion(&ecr.completion);
|
|
|
|
res = ecr.res;
|
|
|
|
}
|
|
|
|
kfree(alloc_buf);
|
2016-01-24 20:17:49 +07:00
|
|
|
skcipher_request_free(req);
|
2015-05-16 06:26:10 +07:00
|
|
|
if (res < 0)
|
2015-04-26 14:12:50 +07:00
|
|
|
printk_ratelimited(KERN_ERR
|
|
|
|
"%s: Error (error code %d)\n", __func__, res);
|
2015-05-16 06:26:10 +07:00
|
|
|
|
2015-04-26 14:12:50 +07:00
|
|
|
oname->len = ciphertext_len;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-05-16 06:26:10 +07:00
|
|
|
* fname_decrypt()
|
2015-04-26 14:12:50 +07:00
|
|
|
* This function decrypts the input filename, and returns
|
|
|
|
* the length of the plaintext.
|
|
|
|
* Errors are returned as negative numbers.
|
|
|
|
* We trust the caller to allocate sufficient memory to oname string.
|
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
static int fname_decrypt(struct inode *inode,
|
|
|
|
const struct fscrypt_str *iname,
|
|
|
|
struct fscrypt_str *oname)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
2016-01-24 20:17:49 +07:00
|
|
|
struct skcipher_request *req = NULL;
|
2015-05-16 06:26:10 +07:00
|
|
|
DECLARE_FS_COMPLETION_RESULT(ecr);
|
2015-04-26 14:12:50 +07:00
|
|
|
struct scatterlist src_sg, dst_sg;
|
2015-05-16 06:26:10 +07:00
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-01-24 20:17:49 +07:00
|
|
|
struct crypto_skcipher *tfm = ci->ci_ctfm;
|
2015-04-26 14:12:50 +07:00
|
|
|
int res = 0;
|
2015-05-16 06:26:10 +07:00
|
|
|
char iv[FS_CRYPTO_BLOCK_SIZE];
|
|
|
|
unsigned lim;
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
lim = inode->i_sb->s_cop->max_namelen(inode);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (iname->len <= 0 || iname->len > lim)
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
/* Allocate request */
|
2016-01-24 20:17:49 +07:00
|
|
|
req = skcipher_request_alloc(tfm, GFP_NOFS);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (!req) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
|
|
|
"%s: crypto_request_alloc() failed\n", __func__);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2016-01-24 20:17:49 +07:00
|
|
|
skcipher_request_set_callback(req,
|
2015-04-26 14:12:50 +07:00
|
|
|
CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
|
2015-05-16 06:26:10 +07:00
|
|
|
dir_crypt_complete, &ecr);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/* Initialize IV */
|
2015-05-16 06:26:10 +07:00
|
|
|
memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
|
2015-04-26 14:12:50 +07: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 20:17:49 +07:00
|
|
|
skcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
|
|
|
|
res = crypto_skcipher_decrypt(req);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (res == -EINPROGRESS || res == -EBUSY) {
|
|
|
|
wait_for_completion(&ecr.completion);
|
|
|
|
res = ecr.res;
|
|
|
|
}
|
2016-01-24 20:17:49 +07:00
|
|
|
skcipher_request_free(req);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (res < 0) {
|
|
|
|
printk_ratelimited(KERN_ERR
|
2015-05-16 06:26:10 +07:00
|
|
|
"%s: Error (error code %d)\n", __func__, res);
|
2015-04-26 14:12:50 +07:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
oname->len = strnlen(oname->name, iname->len);
|
|
|
|
return oname->len;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *lookup_table =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
|
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* digest_encode() -
|
2015-04-26 14:12:50 +07: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;
|
|
|
|
}
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
u32 fscrypt_fname_encrypted_size(struct inode *inode, u32 ilen)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
2016-02-15 16:54:26 +07:00
|
|
|
int padding = 32;
|
2015-05-16 06:26:10 +07:00
|
|
|
struct fscrypt_info *ci = inode->i_crypt_info;
|
2016-02-15 16:54:26 +07:00
|
|
|
|
|
|
|
if (ci)
|
2015-05-16 06:26:10 +07:00
|
|
|
padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
|
|
|
|
if (ilen < FS_CRYPTO_BLOCK_SIZE)
|
|
|
|
ilen = FS_CRYPTO_BLOCK_SIZE;
|
|
|
|
return size_round_up(ilen, padding);
|
2015-04-26 14:12:50 +07:00
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* fscrypt_fname_crypto_alloc_obuff() -
|
2015-04-26 14:12:50 +07:00
|
|
|
*
|
|
|
|
* Allocates an output buffer that is sufficient for the crypto operation
|
|
|
|
* specified by the context and the direction.
|
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
int fscrypt_fname_alloc_buffer(struct inode *inode,
|
|
|
|
u32 ilen, struct fscrypt_str *crypto_str)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
2015-05-16 06:26:10 +07:00
|
|
|
unsigned int olen = fscrypt_fname_encrypted_size(inode, ilen);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
crypto_str->len = olen;
|
2015-05-16 06:26:10 +07:00
|
|
|
if (olen < FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
|
|
|
|
olen = FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
|
|
|
|
/*
|
|
|
|
* Allocated buffer can hold one more character to null-terminate the
|
|
|
|
* string
|
|
|
|
*/
|
2015-04-26 14:12:50 +07:00
|
|
|
crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
|
|
|
|
if (!(crypto_str->name))
|
|
|
|
return -ENOMEM;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* fscrypt_fname_crypto_free_buffer() -
|
2015-04-26 14:12:50 +07:00
|
|
|
*
|
|
|
|
* Frees the buffer allocated for crypto operation.
|
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
|
|
|
if (!crypto_str)
|
|
|
|
return;
|
|
|
|
kfree(crypto_str->name);
|
|
|
|
crypto_str->name = NULL;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_free_buffer);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
|
|
|
|
* space
|
2015-04-26 14:12:50 +07:00
|
|
|
*/
|
2015-05-16 06:26:10 +07: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 14:12:50 +07:00
|
|
|
{
|
|
|
|
const struct qstr qname = FSTR_TO_QSTR(iname);
|
|
|
|
char buf[24];
|
|
|
|
int ret;
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
if (fscrypt_is_dot_dotdot(&qname)) {
|
2015-04-26 14:12:50 +07:00
|
|
|
oname->name[0] = '.';
|
|
|
|
oname->name[iname->len - 1] = '.';
|
|
|
|
oname->len = iname->len;
|
|
|
|
return oname->len;
|
|
|
|
}
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
if (iname->len < FS_CRYPTO_BLOCK_SIZE)
|
2016-02-06 10:37:27 +07:00
|
|
|
return -EUCLEAN;
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
if (inode->i_crypt_info)
|
|
|
|
return fname_decrypt(inode, iname, oname);
|
|
|
|
|
|
|
|
if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
|
2015-04-26 14:12:50 +07:00
|
|
|
ret = digest_encode(iname->name, iname->len, oname->name);
|
|
|
|
oname->len = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (hash) {
|
2015-05-16 06:26:10 +07:00
|
|
|
memcpy(buf, &hash, 4);
|
|
|
|
memcpy(buf + 4, &minor_hash, 4);
|
|
|
|
} else {
|
2015-04-26 14:12:50 +07:00
|
|
|
memset(buf, 0, 8);
|
2015-05-16 06:26:10 +07:00
|
|
|
}
|
2015-04-26 14:12:50 +07:00
|
|
|
memcpy(buf + 8, iname->name + iname->len - 16, 16);
|
|
|
|
oname->name[0] = '_';
|
|
|
|
ret = digest_encode(buf, 24, oname->name + 1);
|
|
|
|
oname->len = ret + 1;
|
|
|
|
return ret + 1;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
|
|
|
/**
|
2015-05-16 06:26:10 +07:00
|
|
|
* fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
|
|
|
|
* space
|
2015-04-26 14:12:50 +07:00
|
|
|
*/
|
2015-05-16 06:26:10 +07:00
|
|
|
int fscrypt_fname_usr_to_disk(struct inode *inode,
|
2015-04-26 14:12:50 +07:00
|
|
|
const struct qstr *iname,
|
2015-05-16 06:26:10 +07:00
|
|
|
struct fscrypt_str *oname)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
2015-05-16 06:26:10 +07:00
|
|
|
if (fscrypt_is_dot_dotdot(iname)) {
|
2015-04-26 14:12:50 +07:00
|
|
|
oname->name[0] = '.';
|
|
|
|
oname->name[iname->len - 1] = '.';
|
|
|
|
oname->len = iname->len;
|
|
|
|
return oname->len;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
if (inode->i_crypt_info)
|
|
|
|
return fname_encrypt(inode, iname, oname);
|
|
|
|
/*
|
|
|
|
* Without a proper key, a user is not allowed to modify the filenames
|
2015-04-26 14:12:50 +07:00
|
|
|
* in a directory. Consequently, a user space name cannot be mapped to
|
2015-05-16 06:26:10 +07:00
|
|
|
* a disk-space name
|
|
|
|
*/
|
2015-04-26 14:12:50 +07:00
|
|
|
return -EACCES;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
|
|
|
|
int lookup, struct fscrypt_name *fname)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
|
|
|
int ret = 0, bigname = 0;
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
memset(fname, 0, sizeof(struct fscrypt_name));
|
2015-04-26 14:12:50 +07:00
|
|
|
fname->usr_fname = iname;
|
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
if (!dir->i_sb->s_cop->is_encrypted(dir) ||
|
|
|
|
fscrypt_is_dot_dotdot(iname)) {
|
2015-04-26 14:12:50 +07:00
|
|
|
fname->disk_name.name = (unsigned char *)iname->name;
|
|
|
|
fname->disk_name.len = iname->len;
|
2015-05-13 17:20:54 +07:00
|
|
|
return 0;
|
2015-04-26 14:12:50 +07:00
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
ret = get_crypt_info(dir);
|
|
|
|
if (ret && ret != -EOPNOTSUPP)
|
2015-04-26 14:12:50 +07:00
|
|
|
return ret;
|
2015-05-16 06:26:10 +07:00
|
|
|
|
|
|
|
if (dir->i_crypt_info) {
|
|
|
|
ret = fscrypt_fname_alloc_buffer(dir, iname->len,
|
|
|
|
&fname->crypto_buf);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (ret < 0)
|
2015-05-13 17:20:54 +07:00
|
|
|
return ret;
|
2015-05-16 06:26:10 +07:00
|
|
|
ret = fname_encrypt(dir, iname, &fname->crypto_buf);
|
2015-04-26 14:12:50 +07:00
|
|
|
if (ret < 0)
|
2015-05-29 07:06:40 +07:00
|
|
|
goto errout;
|
2015-04-26 14:12:50 +07:00
|
|
|
fname->disk_name.name = fname->crypto_buf.name;
|
|
|
|
fname->disk_name.len = fname->crypto_buf.len;
|
2015-05-13 17:20:54 +07:00
|
|
|
return 0;
|
2015-04-26 14:12:50 +07:00
|
|
|
}
|
2015-05-29 07:06:40 +07:00
|
|
|
if (!lookup)
|
|
|
|
return -EACCES;
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
/*
|
|
|
|
* We don't have the key and we are doing a lookup; decode the
|
2015-04-26 14:12:50 +07:00
|
|
|
* user-supplied name
|
|
|
|
*/
|
|
|
|
if (iname->name[0] == '_')
|
|
|
|
bigname = 1;
|
2015-05-16 06:26:10 +07:00
|
|
|
if ((bigname && (iname->len != 33)) || (!bigname && (iname->len > 43)))
|
2015-05-29 07:06:40 +07:00
|
|
|
return -ENOENT;
|
|
|
|
|
2015-04-26 14:12:50 +07:00
|
|
|
fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
|
2015-05-29 07:06:40 +07:00
|
|
|
if (fname->crypto_buf.name == NULL)
|
|
|
|
return -ENOMEM;
|
2015-05-16 06:26:10 +07:00
|
|
|
|
2015-04-26 14:12:50 +07:00
|
|
|
ret = digest_decode(iname->name + bigname, iname->len - bigname,
|
|
|
|
fname->crypto_buf.name);
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -ENOENT;
|
2015-05-29 07:06:40 +07:00
|
|
|
goto errout;
|
2015-04-26 14:12:50 +07:00
|
|
|
}
|
|
|
|
fname->crypto_buf.len = ret;
|
|
|
|
if (bigname) {
|
|
|
|
memcpy(&fname->hash, fname->crypto_buf.name, 4);
|
2015-05-16 06:26:10 +07:00
|
|
|
memcpy(&fname->minor_hash, fname->crypto_buf.name + 4, 4);
|
2015-04-26 14:12:50 +07:00
|
|
|
} else {
|
|
|
|
fname->disk_name.name = fname->crypto_buf.name;
|
|
|
|
fname->disk_name.len = fname->crypto_buf.len;
|
|
|
|
}
|
2015-05-13 17:20:54 +07:00
|
|
|
return 0;
|
2015-05-16 06:26:10 +07:00
|
|
|
|
2015-05-29 07:06:40 +07:00
|
|
|
errout:
|
2015-05-16 06:26:10 +07:00
|
|
|
fscrypt_fname_free_buffer(&fname->crypto_buf);
|
2015-04-26 14:12:50 +07:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_setup_filename);
|
2015-04-26 14:12:50 +07:00
|
|
|
|
2015-05-16 06:26:10 +07:00
|
|
|
void fscrypt_free_filename(struct fscrypt_name *fname)
|
2015-04-26 14:12:50 +07:00
|
|
|
{
|
|
|
|
kfree(fname->crypto_buf.name);
|
|
|
|
fname->crypto_buf.name = NULL;
|
|
|
|
fname->usr_fname = NULL;
|
|
|
|
fname->disk_name.name = NULL;
|
|
|
|
}
|
2015-05-16 06:26:10 +07:00
|
|
|
EXPORT_SYMBOL(fscrypt_free_filename);
|