Commit 7f5fe3ec authored by Linus Torvalds's avatar Linus Torvalds

Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ecryptfs/ecryptfs-2.6

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ecryptfs/ecryptfs-2.6:
  eCryptfs: write lock requested keys
  eCryptfs: move ecryptfs_find_auth_tok_for_sig() call before mutex_lock
  eCryptfs: verify authentication tokens before their use
  eCryptfs: modified size of keysig in the ecryptfs_key_sig structure
  eCryptfs: removed num_global_auth_toks from ecryptfs_mount_crypt_stat
  eCryptfs: ecryptfs_keyring_auth_tok_for_sig() bug fix
  eCryptfs: Unlock page in write_begin error path
  ecryptfs: modify write path to encrypt page in writepage
  eCryptfs: Remove ECRYPTFS_NEW_FILE crypt stat flag
  eCryptfs: Remove unnecessary grow_file() function
parents 212a17ab b5695d04
...@@ -266,7 +266,6 @@ void ecryptfs_destroy_mount_crypt_stat( ...@@ -266,7 +266,6 @@ void ecryptfs_destroy_mount_crypt_stat(
&mount_crypt_stat->global_auth_tok_list, &mount_crypt_stat->global_auth_tok_list,
mount_crypt_stat_list) { mount_crypt_stat_list) {
list_del(&auth_tok->mount_crypt_stat_list); list_del(&auth_tok->mount_crypt_stat_list);
mount_crypt_stat->num_global_auth_toks--;
if (auth_tok->global_auth_tok_key if (auth_tok->global_auth_tok_key
&& !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID)) && !(auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID))
key_put(auth_tok->global_auth_tok_key); key_put(auth_tok->global_auth_tok_key);
...@@ -1389,6 +1388,7 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry) ...@@ -1389,6 +1388,7 @@ int ecryptfs_write_metadata(struct dentry *ecryptfs_dentry)
rc = -ENOMEM; rc = -ENOMEM;
goto out; goto out;
} }
/* Zeroed page ensures the in-header unencrypted i_size is set to 0 */
rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat, rc = ecryptfs_write_headers_virt(virt, virt_len, &size, crypt_stat,
ecryptfs_dentry); ecryptfs_dentry);
if (unlikely(rc)) { if (unlikely(rc)) {
......
...@@ -233,7 +233,7 @@ ecryptfs_get_key_payload_data(struct key *key) ...@@ -233,7 +233,7 @@ ecryptfs_get_key_payload_data(struct key *key)
struct ecryptfs_key_sig { struct ecryptfs_key_sig {
struct list_head crypt_stat_list; struct list_head crypt_stat_list;
char keysig[ECRYPTFS_SIG_SIZE_HEX]; char keysig[ECRYPTFS_SIG_SIZE_HEX + 1];
}; };
struct ecryptfs_filename { struct ecryptfs_filename {
...@@ -257,19 +257,18 @@ struct ecryptfs_filename { ...@@ -257,19 +257,18 @@ struct ecryptfs_filename {
struct ecryptfs_crypt_stat { struct ecryptfs_crypt_stat {
#define ECRYPTFS_STRUCT_INITIALIZED 0x00000001 #define ECRYPTFS_STRUCT_INITIALIZED 0x00000001
#define ECRYPTFS_POLICY_APPLIED 0x00000002 #define ECRYPTFS_POLICY_APPLIED 0x00000002
#define ECRYPTFS_NEW_FILE 0x00000004 #define ECRYPTFS_ENCRYPTED 0x00000004
#define ECRYPTFS_ENCRYPTED 0x00000008 #define ECRYPTFS_SECURITY_WARNING 0x00000008
#define ECRYPTFS_SECURITY_WARNING 0x00000010 #define ECRYPTFS_ENABLE_HMAC 0x00000010
#define ECRYPTFS_ENABLE_HMAC 0x00000020 #define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000020
#define ECRYPTFS_ENCRYPT_IV_PAGES 0x00000040 #define ECRYPTFS_KEY_VALID 0x00000040
#define ECRYPTFS_KEY_VALID 0x00000080 #define ECRYPTFS_METADATA_IN_XATTR 0x00000080
#define ECRYPTFS_METADATA_IN_XATTR 0x00000100 #define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000100
#define ECRYPTFS_VIEW_AS_ENCRYPTED 0x00000200 #define ECRYPTFS_KEY_SET 0x00000200
#define ECRYPTFS_KEY_SET 0x00000400 #define ECRYPTFS_ENCRYPT_FILENAMES 0x00000400
#define ECRYPTFS_ENCRYPT_FILENAMES 0x00000800 #define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00000800
#define ECRYPTFS_ENCFN_USE_MOUNT_FNEK 0x00001000 #define ECRYPTFS_ENCFN_USE_FEK 0x00001000
#define ECRYPTFS_ENCFN_USE_FEK 0x00002000 #define ECRYPTFS_UNLINK_SIGS 0x00002000
#define ECRYPTFS_UNLINK_SIGS 0x00004000
u32 flags; u32 flags;
unsigned int file_version; unsigned int file_version;
size_t iv_bytes; size_t iv_bytes;
...@@ -297,7 +296,6 @@ struct ecryptfs_inode_info { ...@@ -297,7 +296,6 @@ struct ecryptfs_inode_info {
struct inode vfs_inode; struct inode vfs_inode;
struct inode *wii_inode; struct inode *wii_inode;
struct file *lower_file; struct file *lower_file;
struct mutex lower_file_mutex;
struct ecryptfs_crypt_stat crypt_stat; struct ecryptfs_crypt_stat crypt_stat;
}; };
...@@ -333,7 +331,6 @@ struct ecryptfs_global_auth_tok { ...@@ -333,7 +331,6 @@ struct ecryptfs_global_auth_tok {
u32 flags; u32 flags;
struct list_head mount_crypt_stat_list; struct list_head mount_crypt_stat_list;
struct key *global_auth_tok_key; struct key *global_auth_tok_key;
struct ecryptfs_auth_tok *global_auth_tok;
unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1]; unsigned char sig[ECRYPTFS_SIG_SIZE_HEX + 1];
}; };
...@@ -380,7 +377,6 @@ struct ecryptfs_mount_crypt_stat { ...@@ -380,7 +377,6 @@ struct ecryptfs_mount_crypt_stat {
u32 flags; u32 flags;
struct list_head global_auth_tok_list; struct list_head global_auth_tok_list;
struct mutex global_auth_tok_list_mutex; struct mutex global_auth_tok_list_mutex;
size_t num_global_auth_toks;
size_t global_default_cipher_key_size; size_t global_default_cipher_key_size;
size_t global_default_fn_cipher_key_bytes; size_t global_default_fn_cipher_key_bytes;
unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE unsigned char global_default_cipher_name[ECRYPTFS_MAX_CIPHER_NAME_SIZE
......
...@@ -273,7 +273,14 @@ static int ecryptfs_release(struct inode *inode, struct file *file) ...@@ -273,7 +273,14 @@ static int ecryptfs_release(struct inode *inode, struct file *file)
static int static int
ecryptfs_fsync(struct file *file, int datasync) ecryptfs_fsync(struct file *file, int datasync)
{ {
return vfs_fsync(ecryptfs_file_to_lower(file), datasync); int rc = 0;
rc = generic_file_fsync(file, datasync);
if (rc)
goto out;
rc = vfs_fsync(ecryptfs_file_to_lower(file), datasync);
out:
return rc;
} }
static int ecryptfs_fasync(int fd, struct file *file, int flag) static int ecryptfs_fasync(int fd, struct file *file, int flag)
......
...@@ -142,26 +142,6 @@ ecryptfs_do_create(struct inode *directory_inode, ...@@ -142,26 +142,6 @@ ecryptfs_do_create(struct inode *directory_inode,
return rc; return rc;
} }
/**
* grow_file
* @ecryptfs_dentry: the eCryptfs dentry
*
* This is the code which will grow the file to its correct size.
*/
static int grow_file(struct dentry *ecryptfs_dentry)
{
struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
char zero_virt[] = { 0x00 };
int rc = 0;
rc = ecryptfs_write(ecryptfs_inode, zero_virt, 0, 1);
i_size_write(ecryptfs_inode, 0);
rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
ECRYPTFS_NEW_FILE;
return rc;
}
/** /**
* ecryptfs_initialize_file * ecryptfs_initialize_file
* *
...@@ -181,7 +161,6 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry) ...@@ -181,7 +161,6 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
goto out; goto out;
} }
crypt_stat->flags |= ECRYPTFS_NEW_FILE;
ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n"); ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
rc = ecryptfs_new_file_context(ecryptfs_dentry); rc = ecryptfs_new_file_context(ecryptfs_dentry);
if (rc) { if (rc) {
...@@ -202,9 +181,6 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry) ...@@ -202,9 +181,6 @@ static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc); printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
goto out; goto out;
} }
rc = grow_file(ecryptfs_dentry);
if (rc)
printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
out: out:
return rc; return rc;
} }
......
...@@ -65,6 +65,24 @@ static int process_request_key_err(long err_code) ...@@ -65,6 +65,24 @@ static int process_request_key_err(long err_code)
return rc; return rc;
} }
static int process_find_global_auth_tok_for_sig_err(int err_code)
{
int rc = err_code;
switch (err_code) {
case -ENOENT:
ecryptfs_printk(KERN_WARNING, "Missing auth tok\n");
break;
case -EINVAL:
ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n");
break;
default:
rc = process_request_key_err(err_code);
break;
}
return rc;
}
/** /**
* ecryptfs_parse_packet_length * ecryptfs_parse_packet_length
* @data: Pointer to memory containing length at offset * @data: Pointer to memory containing length at offset
...@@ -403,27 +421,120 @@ parse_tag_67_packet(struct ecryptfs_key_record *key_rec, ...@@ -403,27 +421,120 @@ parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
return rc; return rc;
} }
/**
* ecryptfs_verify_version
* @version: The version number to confirm
*
* Returns zero on good version; non-zero otherwise
*/
static int ecryptfs_verify_version(u16 version)
{
int rc = 0;
unsigned char major;
unsigned char minor;
major = ((version >> 8) & 0xFF);
minor = (version & 0xFF);
if (major != ECRYPTFS_VERSION_MAJOR) {
ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
"Expected [%d]; got [%d]\n",
ECRYPTFS_VERSION_MAJOR, major);
rc = -EINVAL;
goto out;
}
if (minor != ECRYPTFS_VERSION_MINOR) {
ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
"Expected [%d]; got [%d]\n",
ECRYPTFS_VERSION_MINOR, minor);
rc = -EINVAL;
goto out;
}
out:
return rc;
}
/**
* ecryptfs_verify_auth_tok_from_key
* @auth_tok_key: key containing the authentication token
* @auth_tok: authentication token
*
* Returns zero on valid auth tok; -EINVAL otherwise
*/
static int
ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key,
struct ecryptfs_auth_tok **auth_tok)
{
int rc = 0;
(*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key);
if (ecryptfs_verify_version((*auth_tok)->version)) {
printk(KERN_ERR "Data structure version mismatch. Userspace "
"tools must match eCryptfs kernel module with major "
"version [%d] and minor version [%d]\n",
ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR);
rc = -EINVAL;
goto out;
}
if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
&& (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
printk(KERN_ERR "Invalid auth_tok structure "
"returned from key query\n");
rc = -EINVAL;
goto out;
}
out:
return rc;
}
static int static int
ecryptfs_find_global_auth_tok_for_sig( ecryptfs_find_global_auth_tok_for_sig(
struct ecryptfs_global_auth_tok **global_auth_tok, struct key **auth_tok_key,
struct ecryptfs_auth_tok **auth_tok,
struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
{ {
struct ecryptfs_global_auth_tok *walker; struct ecryptfs_global_auth_tok *walker;
int rc = 0; int rc = 0;
(*global_auth_tok) = NULL; (*auth_tok_key) = NULL;
(*auth_tok) = NULL;
mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
list_for_each_entry(walker, list_for_each_entry(walker,
&mount_crypt_stat->global_auth_tok_list, &mount_crypt_stat->global_auth_tok_list,
mount_crypt_stat_list) { mount_crypt_stat_list) {
if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX))
rc = key_validate(walker->global_auth_tok_key); continue;
if (!rc)
(*global_auth_tok) = walker; if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) {
rc = -EINVAL;
goto out; goto out;
} }
rc = key_validate(walker->global_auth_tok_key);
if (rc) {
if (rc == -EKEYEXPIRED)
goto out;
goto out_invalid_auth_tok;
}
down_write(&(walker->global_auth_tok_key->sem));
rc = ecryptfs_verify_auth_tok_from_key(
walker->global_auth_tok_key, auth_tok);
if (rc)
goto out_invalid_auth_tok_unlock;
(*auth_tok_key) = walker->global_auth_tok_key;
key_get(*auth_tok_key);
goto out;
} }
rc = -EINVAL; rc = -ENOENT;
goto out;
out_invalid_auth_tok_unlock:
up_write(&(walker->global_auth_tok_key->sem));
out_invalid_auth_tok:
printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig);
walker->flags |= ECRYPTFS_AUTH_TOK_INVALID;
key_put(walker->global_auth_tok_key);
walker->global_auth_tok_key = NULL;
out: out:
mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
return rc; return rc;
...@@ -451,14 +562,11 @@ ecryptfs_find_auth_tok_for_sig( ...@@ -451,14 +562,11 @@ ecryptfs_find_auth_tok_for_sig(
struct ecryptfs_mount_crypt_stat *mount_crypt_stat, struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
char *sig) char *sig)
{ {
struct ecryptfs_global_auth_tok *global_auth_tok;
int rc = 0; int rc = 0;
(*auth_tok_key) = NULL; rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok,
(*auth_tok) = NULL; mount_crypt_stat, sig);
if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, if (rc == -ENOENT) {
mount_crypt_stat, sig)) {
/* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the
* mount_crypt_stat structure, we prevent to use auth toks that * mount_crypt_stat structure, we prevent to use auth toks that
* are not inserted through the ecryptfs_add_global_auth_tok * are not inserted through the ecryptfs_add_global_auth_tok
...@@ -470,8 +578,7 @@ ecryptfs_find_auth_tok_for_sig( ...@@ -470,8 +578,7 @@ ecryptfs_find_auth_tok_for_sig(
rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok,
sig); sig);
} else }
(*auth_tok) = global_auth_tok->global_auth_tok;
return rc; return rc;
} }
...@@ -531,6 +638,16 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, ...@@ -531,6 +638,16 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
} }
s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; s->desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
(*packet_size) = 0; (*packet_size) = 0;
rc = ecryptfs_find_auth_tok_for_sig(
&auth_tok_key,
&s->auth_tok, mount_crypt_stat,
mount_crypt_stat->global_default_fnek_sig);
if (rc) {
printk(KERN_ERR "%s: Error attempting to find auth tok for "
"fnek sig [%s]; rc = [%d]\n", __func__,
mount_crypt_stat->global_default_fnek_sig, rc);
goto out;
}
rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(
&s->desc.tfm, &s->desc.tfm,
&s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name);
...@@ -616,16 +733,6 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, ...@@ -616,16 +733,6 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
goto out_free_unlock; goto out_free_unlock;
} }
dest[s->i++] = s->cipher_code; dest[s->i++] = s->cipher_code;
rc = ecryptfs_find_auth_tok_for_sig(
&auth_tok_key,
&s->auth_tok, mount_crypt_stat,
mount_crypt_stat->global_default_fnek_sig);
if (rc) {
printk(KERN_ERR "%s: Error attempting to find auth tok for "
"fnek sig [%s]; rc = [%d]\n", __func__,
mount_crypt_stat->global_default_fnek_sig, rc);
goto out_free_unlock;
}
/* TODO: Support other key modules than passphrase for /* TODO: Support other key modules than passphrase for
* filename encryption */ * filename encryption */
if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
...@@ -765,8 +872,10 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, ...@@ -765,8 +872,10 @@ ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes,
out_unlock: out_unlock:
mutex_unlock(s->tfm_mutex); mutex_unlock(s->tfm_mutex);
out: out:
if (auth_tok_key) if (auth_tok_key) {
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key); key_put(auth_tok_key);
}
kfree(s); kfree(s);
return rc; return rc;
} }
...@@ -879,6 +988,15 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, ...@@ -879,6 +988,15 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
__func__, s->cipher_code); __func__, s->cipher_code);
goto out; goto out;
} }
rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
&s->auth_tok, mount_crypt_stat,
s->fnek_sig_hex);
if (rc) {
printk(KERN_ERR "%s: Error attempting to find auth tok for "
"fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
rc);
goto out;
}
rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm, rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->desc.tfm,
&s->tfm_mutex, &s->tfm_mutex,
s->cipher_string); s->cipher_string);
...@@ -925,15 +1043,6 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, ...@@ -925,15 +1043,6 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
* >= ECRYPTFS_MAX_IV_BYTES. */ * >= ECRYPTFS_MAX_IV_BYTES. */
memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES); memset(s->iv, 0, ECRYPTFS_MAX_IV_BYTES);
s->desc.info = s->iv; s->desc.info = s->iv;
rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key,
&s->auth_tok, mount_crypt_stat,
s->fnek_sig_hex);
if (rc) {
printk(KERN_ERR "%s: Error attempting to find auth tok for "
"fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex,
rc);
goto out_free_unlock;
}
/* TODO: Support other key modules than passphrase for /* TODO: Support other key modules than passphrase for
* filename encryption */ * filename encryption */
if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) {
...@@ -1002,8 +1111,10 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, ...@@ -1002,8 +1111,10 @@ ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size,
(*filename_size) = 0; (*filename_size) = 0;
(*filename) = NULL; (*filename) = NULL;
} }
if (auth_tok_key) if (auth_tok_key) {
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key); key_put(auth_tok_key);
}
kfree(s); kfree(s);
return rc; return rc;
} }
...@@ -1520,38 +1631,6 @@ parse_tag_11_packet(unsigned char *data, unsigned char *contents, ...@@ -1520,38 +1631,6 @@ parse_tag_11_packet(unsigned char *data, unsigned char *contents,
return rc; return rc;
} }
/**
* ecryptfs_verify_version
* @version: The version number to confirm
*
* Returns zero on good version; non-zero otherwise
*/
static int ecryptfs_verify_version(u16 version)
{
int rc = 0;
unsigned char major;
unsigned char minor;
major = ((version >> 8) & 0xFF);
minor = (version & 0xFF);
if (major != ECRYPTFS_VERSION_MAJOR) {
ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
"Expected [%d]; got [%d]\n",
ECRYPTFS_VERSION_MAJOR, major);
rc = -EINVAL;
goto out;
}
if (minor != ECRYPTFS_VERSION_MINOR) {
ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
"Expected [%d]; got [%d]\n",
ECRYPTFS_VERSION_MINOR, minor);
rc = -EINVAL;
goto out;
}
out:
return rc;
}
int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
struct ecryptfs_auth_tok **auth_tok, struct ecryptfs_auth_tok **auth_tok,
char *sig) char *sig)
...@@ -1563,31 +1642,16 @@ int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, ...@@ -1563,31 +1642,16 @@ int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
printk(KERN_ERR "Could not find key with description: [%s]\n", printk(KERN_ERR "Could not find key with description: [%s]\n",
sig); sig);
rc = process_request_key_err(PTR_ERR(*auth_tok_key)); rc = process_request_key_err(PTR_ERR(*auth_tok_key));
(*auth_tok_key) = NULL;
goto out; goto out;
} }
(*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); down_write(&(*auth_tok_key)->sem);
if (ecryptfs_verify_version((*auth_tok)->version)) { rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok);
printk(KERN_ERR
"Data structure version mismatch. "
"Userspace tools must match eCryptfs "
"kernel module with major version [%d] "
"and minor version [%d]\n",
ECRYPTFS_VERSION_MAJOR,
ECRYPTFS_VERSION_MINOR);
rc = -EINVAL;
goto out_release_key;
}
if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
&& (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
printk(KERN_ERR "Invalid auth_tok structure "
"returned from key query\n");
rc = -EINVAL;
goto out_release_key;
}
out_release_key:
if (rc) { if (rc) {
up_write(&(*auth_tok_key)->sem);
key_put(*auth_tok_key); key_put(*auth_tok_key);
(*auth_tok_key) = NULL; (*auth_tok_key) = NULL;
goto out;
} }
out: out:
return rc; return rc;
...@@ -1809,6 +1873,7 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, ...@@ -1809,6 +1873,7 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
find_next_matching_auth_tok: find_next_matching_auth_tok:
found_auth_tok = 0; found_auth_tok = 0;
if (auth_tok_key) { if (auth_tok_key) {
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key); key_put(auth_tok_key);
auth_tok_key = NULL; auth_tok_key = NULL;
} }
...@@ -1895,8 +1960,10 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, ...@@ -1895,8 +1960,10 @@ int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
out_wipe_list: out_wipe_list:
wipe_auth_tok_list(&auth_tok_list); wipe_auth_tok_list(&auth_tok_list);
out: out:
if (auth_tok_key) if (auth_tok_key) {
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key); key_put(auth_tok_key);
}
return rc; return rc;
} }
...@@ -2324,7 +2391,7 @@ ecryptfs_generate_key_packet_set(char *dest_base, ...@@ -2324,7 +2391,7 @@ ecryptfs_generate_key_packet_set(char *dest_base,
size_t max) size_t max)
{ {
struct ecryptfs_auth_tok *auth_tok; struct ecryptfs_auth_tok *auth_tok;
struct ecryptfs_global_auth_tok *global_auth_tok; struct key *auth_tok_key = NULL;
struct ecryptfs_mount_crypt_stat *mount_crypt_stat = struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
&ecryptfs_superblock_to_private( &ecryptfs_superblock_to_private(
ecryptfs_dentry->d_sb)->mount_crypt_stat; ecryptfs_dentry->d_sb)->mount_crypt_stat;
...@@ -2343,21 +2410,16 @@ ecryptfs_generate_key_packet_set(char *dest_base, ...@@ -2343,21 +2410,16 @@ ecryptfs_generate_key_packet_set(char *dest_base,
list_for_each_entry(key_sig, &crypt_stat->keysig_list, list_for_each_entry(key_sig, &crypt_stat->keysig_list,
crypt_stat_list) { crypt_stat_list) {
memset(key_rec, 0, sizeof(*key_rec)); memset(key_rec, 0, sizeof(*key_rec));
rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key,
&auth_tok,
mount_crypt_stat, mount_crypt_stat,
key_sig->keysig); key_sig->keysig);
if (rc) { if (rc) {
printk(KERN_ERR "Error attempting to get the global " printk(KERN_WARNING "Unable to retrieve auth tok with "
"auth_tok; rc = [%d]\n", rc); "sig = [%s]\n", key_sig->keysig);
rc = process_find_global_auth_tok_for_sig_err(rc);
goto out_free; goto out_free;
} }
if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
printk(KERN_WARNING
"Skipping invalid auth tok with sig = [%s]\n",
global_auth_tok->sig);
continue;
}
auth_tok = global_auth_tok->global_auth_tok;
if (auth_tok->token_type == ECRYPTFS_PASSWORD) { if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
rc = write_tag_3_packet((dest_base + (*len)), rc = write_tag_3_packet((dest_base + (*len)),
&max, auth_tok, &max, auth_tok,
...@@ -2395,6 +2457,9 @@ ecryptfs_generate_key_packet_set(char *dest_base, ...@@ -2395,6 +2457,9 @@ ecryptfs_generate_key_packet_set(char *dest_base,
rc = -EINVAL; rc = -EINVAL;
goto out_free; goto out_free;
} }
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key);
auth_tok_key = NULL;
} }
if (likely(max > 0)) { if (likely(max > 0)) {
dest_base[(*len)] = 0x00; dest_base[(*len)] = 0x00;
...@@ -2407,6 +2472,11 @@ ecryptfs_generate_key_packet_set(char *dest_base, ...@@ -2407,6 +2472,11 @@ ecryptfs_generate_key_packet_set(char *dest_base,
out: out:
if (rc) if (rc)
(*len) = 0; (*len) = 0;
if (auth_tok_key) {
up_write(&(auth_tok_key->sem));
key_put(auth_tok_key);
}
mutex_unlock(&crypt_stat->keysig_list_mutex); mutex_unlock(&crypt_stat->keysig_list_mutex);
return rc; return rc;
} }
...@@ -2424,6 +2494,7 @@ int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) ...@@ -2424,6 +2494,7 @@ int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
return -ENOMEM; return -ENOMEM;
} }
memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
/* Caller must hold keysig_list_mutex */ /* Caller must hold keysig_list_mutex */
list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
...@@ -2453,7 +2524,6 @@ ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, ...@@ -2453,7 +2524,6 @@ ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
list_add(&new_auth_tok->mount_crypt_stat_list, list_add(&new_auth_tok->mount_crypt_stat_list,
&mount_crypt_stat->global_auth_tok_list); &mount_crypt_stat->global_auth_tok_list);
mount_crypt_stat->num_global_auth_toks++;
mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
out: out:
return rc; return rc;
......
...@@ -122,7 +122,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) ...@@ -122,7 +122,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); ecryptfs_inode_to_private(ecryptfs_dentry->d_inode);
int rc = 0; int rc = 0;
mutex_lock(&inode_info->lower_file_mutex);
if (!inode_info->lower_file) { if (!inode_info->lower_file) {
struct dentry *lower_dentry; struct dentry *lower_dentry;
struct vfsmount *lower_mnt = struct vfsmount *lower_mnt =
...@@ -138,7 +137,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) ...@@ -138,7 +137,6 @@ int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry)
inode_info->lower_file = NULL; inode_info->lower_file = NULL;
} }
} }
mutex_unlock(&inode_info->lower_file_mutex);
return rc; return rc;
} }
...@@ -241,14 +239,14 @@ static int ecryptfs_init_global_auth_toks( ...@@ -241,14 +239,14 @@ static int ecryptfs_init_global_auth_toks(
struct ecryptfs_mount_crypt_stat *mount_crypt_stat) struct ecryptfs_mount_crypt_stat *mount_crypt_stat)
{ {
struct ecryptfs_global_auth_tok *global_auth_tok; struct ecryptfs_global_auth_tok *global_auth_tok;
struct ecryptfs_auth_tok *auth_tok;
int rc = 0; int rc = 0;
list_for_each_entry(global_auth_tok, list_for_each_entry(global_auth_tok,
&mount_crypt_stat->global_auth_tok_list, &mount_crypt_stat->global_auth_tok_list,
mount_crypt_stat_list) { mount_crypt_stat_list) {
rc = ecryptfs_keyring_auth_tok_for_sig( rc = ecryptfs_keyring_auth_tok_for_sig(
&global_auth_tok->global_auth_tok_key, &global_auth_tok->global_auth_tok_key, &auth_tok,
&global_auth_tok->global_auth_tok,
global_auth_tok->sig); global_auth_tok->sig);
if (rc) { if (rc) {
printk(KERN_ERR "Could not find valid key in user " printk(KERN_ERR "Could not find valid key in user "
...@@ -256,8 +254,10 @@ static int ecryptfs_init_global_auth_toks( ...@@ -256,8 +254,10 @@ static int ecryptfs_init_global_auth_toks(
"option: [%s]\n", global_auth_tok->sig); "option: [%s]\n", global_auth_tok->sig);
global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID;
goto out; goto out;
} else } else {
global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID;
up_write(&(global_auth_tok->global_auth_tok_key)->sem);
}
} }
out: out:
return rc; return rc;
......
...@@ -62,6 +62,18 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) ...@@ -62,6 +62,18 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
{ {
int rc; int rc;
/*
* Refuse to write the page out if we are called from reclaim context
* since our writepage() path may potentially allocate memory when
* calling into the lower fs vfs_write() which may in turn invoke
* us again.
*/
if (current->flags & PF_MEMALLOC) {
redirty_page_for_writepage(wbc, page);
rc = 0;
goto out;
}
rc = ecryptfs_encrypt_page(page); rc = ecryptfs_encrypt_page(page);
if (rc) { if (rc) {
ecryptfs_printk(KERN_WARNING, "Error encrypting " ecryptfs_printk(KERN_WARNING, "Error encrypting "
...@@ -70,8 +82,8 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc) ...@@ -70,8 +82,8 @@ static int ecryptfs_writepage(struct page *page, struct writeback_control *wbc)
goto out; goto out;
} }
SetPageUptodate(page); SetPageUptodate(page);
unlock_page(page);
out: out:
unlock_page(page);
return rc; return rc;
} }
...@@ -193,11 +205,7 @@ static int ecryptfs_readpage(struct file *file, struct page *page) ...@@ -193,11 +205,7 @@ static int ecryptfs_readpage(struct file *file, struct page *page)
&ecryptfs_inode_to_private(page->mapping->host)->crypt_stat; &ecryptfs_inode_to_private(page->mapping->host)->crypt_stat;
int rc = 0; int rc = 0;
if (!crypt_stat if (!crypt_stat || !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
|| !(crypt_stat->flags & ECRYPTFS_ENCRYPTED)
|| (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
ecryptfs_printk(KERN_DEBUG,
"Passing through unencrypted page\n");
rc = ecryptfs_read_lower_page_segment(page, page->index, 0, rc = ecryptfs_read_lower_page_segment(page, page->index, 0,
PAGE_CACHE_SIZE, PAGE_CACHE_SIZE,
page->mapping->host); page->mapping->host);
...@@ -295,8 +303,7 @@ static int ecryptfs_write_begin(struct file *file, ...@@ -295,8 +303,7 @@ static int ecryptfs_write_begin(struct file *file,
struct ecryptfs_crypt_stat *crypt_stat = struct ecryptfs_crypt_stat *crypt_stat =
&ecryptfs_inode_to_private(mapping->host)->crypt_stat; &ecryptfs_inode_to_private(mapping->host)->crypt_stat;
if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED) if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
|| (crypt_stat->flags & ECRYPTFS_NEW_FILE)) {
rc = ecryptfs_read_lower_page_segment( rc = ecryptfs_read_lower_page_segment(
page, index, 0, PAGE_CACHE_SIZE, mapping->host); page, index, 0, PAGE_CACHE_SIZE, mapping->host);
if (rc) { if (rc) {
...@@ -374,6 +381,11 @@ static int ecryptfs_write_begin(struct file *file, ...@@ -374,6 +381,11 @@ static int ecryptfs_write_begin(struct file *file,
&& (pos != 0)) && (pos != 0))
zero_user(page, 0, PAGE_CACHE_SIZE); zero_user(page, 0, PAGE_CACHE_SIZE);
out: out:
if (unlikely(rc)) {
unlock_page(page);
page_cache_release(page);
*pagep = NULL;
}
return rc; return rc;
} }
...@@ -486,13 +498,8 @@ static int ecryptfs_write_end(struct file *file, ...@@ -486,13 +498,8 @@ static int ecryptfs_write_end(struct file *file,
struct ecryptfs_crypt_stat *crypt_stat = struct ecryptfs_crypt_stat *crypt_stat =
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
int rc; int rc;
int need_unlock_page = 1;
if (crypt_stat->flags & ECRYPTFS_NEW_FILE) {
ecryptfs_printk(KERN_DEBUG, "ECRYPTFS_NEW_FILE flag set in "
"crypt_stat at memory location [%p]\n", crypt_stat);
crypt_stat->flags &= ~(ECRYPTFS_NEW_FILE);
} else
ecryptfs_printk(KERN_DEBUG, "Not a new file\n");
ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page" ecryptfs_printk(KERN_DEBUG, "Calling fill_zeros_to_end_of_page"
"(page w/ index = [0x%.16lx], to = [%d])\n", index, to); "(page w/ index = [0x%.16lx], to = [%d])\n", index, to);
if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
...@@ -512,26 +519,26 @@ static int ecryptfs_write_end(struct file *file, ...@@ -512,26 +519,26 @@ static int ecryptfs_write_end(struct file *file,
"zeros in page with index = [0x%.16lx]\n", index); "zeros in page with index = [0x%.16lx]\n", index);
goto out; goto out;
} }
rc = ecryptfs_encrypt_page(page); set_page_dirty(page);
if (rc) { unlock_page(page);
ecryptfs_printk(KERN_WARNING, "Error encrypting page (upper " need_unlock_page = 0;
"index [0x%.16lx])\n", index);
goto out;
}
if (pos + copied > i_size_read(ecryptfs_inode)) { if (pos + copied > i_size_read(ecryptfs_inode)) {
i_size_write(ecryptfs_inode, pos + copied); i_size_write(ecryptfs_inode, pos + copied);
ecryptfs_printk(KERN_DEBUG, "Expanded file size to " ecryptfs_printk(KERN_DEBUG, "Expanded file size to "
"[0x%.16llx]\n", "[0x%.16llx]\n",
(unsigned long long)i_size_read(ecryptfs_inode)); (unsigned long long)i_size_read(ecryptfs_inode));
balance_dirty_pages_ratelimited(mapping);
rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
if (rc) {
printk(KERN_ERR "Error writing inode size to metadata; "
"rc = [%d]\n", rc);
goto out;
}
} }
rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode); rc = copied;
if (rc)
printk(KERN_ERR "Error writing inode size to metadata; "
"rc = [%d]\n", rc);
else
rc = copied;
out: out:
unlock_page(page); if (need_unlock_page)
unlock_page(page);
page_cache_release(page); page_cache_release(page);
return rc; return rc;
} }
......
...@@ -44,15 +44,11 @@ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, ...@@ -44,15 +44,11 @@ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data,
ssize_t rc; ssize_t rc;
inode_info = ecryptfs_inode_to_private(ecryptfs_inode); inode_info = ecryptfs_inode_to_private(ecryptfs_inode);
mutex_lock(&inode_info->lower_file_mutex);
BUG_ON(!inode_info->lower_file); BUG_ON(!inode_info->lower_file);
inode_info->lower_file->f_pos = offset;
fs_save = get_fs(); fs_save = get_fs();
set_fs(get_ds()); set_fs(get_ds());
rc = vfs_write(inode_info->lower_file, data, size, rc = vfs_write(inode_info->lower_file, data, size, &offset);
&inode_info->lower_file->f_pos);
set_fs(fs_save); set_fs(fs_save);
mutex_unlock(&inode_info->lower_file_mutex);
mark_inode_dirty_sync(ecryptfs_inode); mark_inode_dirty_sync(ecryptfs_inode);
return rc; return rc;
} }
...@@ -234,15 +230,11 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size, ...@@ -234,15 +230,11 @@ int ecryptfs_read_lower(char *data, loff_t offset, size_t size,
mm_segment_t fs_save; mm_segment_t fs_save;
ssize_t rc; ssize_t rc;
mutex_lock(&inode_info->lower_file_mutex);
BUG_ON(!inode_info->lower_file); BUG_ON(!inode_info->lower_file);
inode_info->lower_file->f_pos = offset;
fs_save = get_fs(); fs_save = get_fs();
set_fs(get_ds()); set_fs(get_ds());
rc = vfs_read(inode_info->lower_file, data, size, rc = vfs_read(inode_info->lower_file, data, size, &offset);
&inode_info->lower_file->f_pos);
set_fs(fs_save); set_fs(fs_save);
mutex_unlock(&inode_info->lower_file_mutex);
return rc; return rc;
} }
......
...@@ -55,7 +55,6 @@ static struct inode *ecryptfs_alloc_inode(struct super_block *sb) ...@@ -55,7 +55,6 @@ static struct inode *ecryptfs_alloc_inode(struct super_block *sb)
if (unlikely(!inode_info)) if (unlikely(!inode_info))
goto out; goto out;
ecryptfs_init_crypt_stat(&inode_info->crypt_stat); ecryptfs_init_crypt_stat(&inode_info->crypt_stat);
mutex_init(&inode_info->lower_file_mutex);
inode_info->lower_file = NULL; inode_info->lower_file = NULL;
inode = &inode_info->vfs_inode; inode = &inode_info->vfs_inode;
out: out:
...@@ -198,7 +197,7 @@ static int ecryptfs_show_options(struct seq_file *m, struct vfsmount *mnt) ...@@ -198,7 +197,7 @@ static int ecryptfs_show_options(struct seq_file *m, struct vfsmount *mnt)
const struct super_operations ecryptfs_sops = { const struct super_operations ecryptfs_sops = {
.alloc_inode = ecryptfs_alloc_inode, .alloc_inode = ecryptfs_alloc_inode,
.destroy_inode = ecryptfs_destroy_inode, .destroy_inode = ecryptfs_destroy_inode,
.drop_inode = generic_delete_inode, .drop_inode = generic_drop_inode,
.statfs = ecryptfs_statfs, .statfs = ecryptfs_statfs,
.remount_fs = NULL, .remount_fs = NULL,
.evict_inode = ecryptfs_evict_inode, .evict_inode = ecryptfs_evict_inode,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment