rc = lock_parent(dentry, &lower_dentry, &lower_dir);
dget(lower_dentry); // don't even try to make the lower negative if (!rc) { if (d_unhashed(lower_dentry))
rc = -EINVAL; else
rc = vfs_unlink(&nop_mnt_idmap, lower_dir, lower_dentry,
NULL);
} if (rc) {
printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc); goto out_unlock;
}
fsstack_copy_attr_times(dir, lower_dir);
set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
inode_set_ctime_to_ts(inode, inode_get_ctime(dir));
out_unlock:
dput(lower_dentry);
inode_unlock(lower_dir); if (!rc)
d_drop(dentry); return rc;
}
/** * ecryptfs_do_create * @directory_inode: inode of the new file's dentry's parent in ecryptfs * @ecryptfs_dentry: New file's dentry in ecryptfs * @mode: The mode of the new file * * Creates the underlying file and the eCryptfs inode which will link to * it. It will also update the eCryptfs directory inode to mimic the * stat of the lower directory inode. * * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
*/ staticstruct inode *
ecryptfs_do_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry, umode_t mode)
{ int rc; struct dentry *lower_dentry; struct inode *lower_dir; struct inode *inode;
/* * ecryptfs_initialize_file * * Cause the file to be changed from a basic empty file to an ecryptfs * file with a header and first data page. * * Returns zero on success
*/ int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry, struct inode *ecryptfs_inode)
{ struct ecryptfs_crypt_stat *crypt_stat =
&ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat; int rc = 0;
if (S_ISDIR(ecryptfs_inode->i_mode)) {
ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); goto out;
}
ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
rc = ecryptfs_new_file_context(ecryptfs_inode); if (rc) {
ecryptfs_printk(KERN_ERR, "Error creating new file " "context; rc = [%d]\n", rc); goto out;
}
rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode); if (rc) {
printk(KERN_ERR "%s: Error attempting to initialize " "the lower file for the dentry with name " "[%pd]; rc = [%d]\n", __func__,
ecryptfs_dentry, rc); goto out;
}
rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode); if (rc)
printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
ecryptfs_put_lower_file(ecryptfs_inode);
out: return rc;
}
/* * ecryptfs_create * @mode: The mode of the new file. * * Creates a new file. * * Returns zero on success; non-zero on error condition
*/ staticint
ecryptfs_create(struct mnt_idmap *idmap, struct inode *directory_inode, struct dentry *ecryptfs_dentry,
umode_t mode, bool excl)
{ struct inode *ecryptfs_inode; int rc;
ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
mode); if (IS_ERR(ecryptfs_inode)) {
ecryptfs_printk(KERN_WARNING, "Failed to create file in" "lower filesystem\n");
rc = PTR_ERR(ecryptfs_inode); goto out;
} /* At this point, a file exists on "disk"; we need to make sure
* that this on disk file is prepared to be an ecryptfs file */
rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode); if (rc) {
ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
ecryptfs_inode);
iget_failed(ecryptfs_inode); goto out;
}
d_instantiate_new(ecryptfs_dentry, ecryptfs_inode);
out: return rc;
}
rc = ecryptfs_get_lower_file(dentry, inode); if (rc) {
printk(KERN_ERR "%s: Error attempting to initialize " "the lower file for the dentry with name " "[%pd]; rc = [%d]\n", __func__,
dentry, rc); return rc;
}
crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; /* TODO: lock for crypt_stat comparison */ if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
ecryptfs_set_default_sizes(crypt_stat);
rc = ecryptfs_read_and_validate_header_region(inode);
ecryptfs_put_lower_file(inode); if (rc) {
rc = ecryptfs_read_and_validate_xattr_region(dentry, inode); if (!rc)
crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
}
/* Must return 0 to allow non-eCryptfs files to be looked up, too */ return 0;
}
/* * negative dentry can go positive under us here - its parent is not * locked. That's OK and that could happen just as we return from * ecryptfs_lookup() anyway. Just need to be careful and fetch * ->d_inode only once - it's not stable here.
*/
lower_inode = READ_ONCE(lower_dentry->d_inode);
if (!lower_inode) { /* We want to add because we couldn't find in lower */
d_add(dentry, NULL); return NULL;
}
inode = __ecryptfs_get_inode(lower_inode, dentry->d_sb); if (IS_ERR(inode)) {
printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
__func__, PTR_ERR(inode)); return ERR_CAST(inode);
} if (S_ISREG(inode->i_mode)) {
rc = ecryptfs_i_size_read(dentry, inode); if (rc) {
make_bad_inode(inode); return ERR_PTR(rc);
}
}
if (inode->i_state & I_NEW)
unlock_new_inode(inode); return d_splice_alias(inode, dentry);
}
/** * ecryptfs_lookup * @ecryptfs_dir_inode: The eCryptfs directory inode * @ecryptfs_dentry: The eCryptfs dentry that we are looking up * @flags: lookup flags * * Find a file on disk. If the file does not exist, then we'll add it to the * dentry cache and continue on to read it from the disk.
*/ staticstruct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode, struct dentry *ecryptfs_dentry, unsignedint flags)
{ char *encrypted_and_encoded_name = NULL; struct ecryptfs_mount_crypt_stat *mount_crypt_stat; struct dentry *lower_dir_dentry, *lower_dentry; struct qstr qname = QSTR_INIT(ecryptfs_dentry->d_name.name,
ecryptfs_dentry->d_name.len); struct dentry *res; int rc = 0;
trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry); if (IS_ERR(trap)) return PTR_ERR(trap);
dget(lower_new_dentry);
rc = -EINVAL; if (lower_old_dentry->d_parent != lower_old_dir_dentry) goto out_lock; if (lower_new_dentry->d_parent != lower_new_dir_dentry) goto out_lock; if (d_unhashed(lower_old_dentry) || d_unhashed(lower_new_dentry)) goto out_lock; /* source should not be ancestor of target */ if (trap == lower_old_dentry) goto out_lock; /* target should not be ancestor of source */ if (trap == lower_new_dentry) {
rc = -ENOTEMPTY; goto out_lock;
}
/** * upper_size_to_lower_size * @crypt_stat: Crypt_stat associated with file * @upper_size: Size of the upper file * * Calculate the required size of the lower file based on the * specified size of the upper file. This calculation is based on the * number of headers in the underlying file and the extent size. * * Returns Calculated size of the lower file.
*/ static loff_t
upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
loff_t upper_size)
{
loff_t lower_size;
lower_size = ecryptfs_lower_header_size(crypt_stat); if (upper_size != 0) {
loff_t num_extents;
/** * truncate_upper * @dentry: The ecryptfs layer dentry * @ia: Address of the ecryptfs inode's attributes * @lower_ia: Address of the lower inode's attributes * * Function to handle truncations modifying the size of the file. Note * that the file sizes are interpolated. When expanding, we are simply * writing strings of 0's out. When truncating, we truncate the upper * inode and update the lower_ia according to the page index * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return, * the caller must use lower_ia in a call to notify_change() to perform * the truncation of the lower inode. * * Returns zero on success; non-zero otherwise
*/ staticint truncate_upper(struct dentry *dentry, struct iattr *ia, struct iattr *lower_ia)
{ int rc = 0; struct inode *inode = d_inode(dentry); struct ecryptfs_crypt_stat *crypt_stat;
loff_t i_size = i_size_read(inode);
loff_t lower_size_before_truncate;
loff_t lower_size_after_truncate;
if (unlikely((ia->ia_size == i_size))) {
lower_ia->ia_valid &= ~ATTR_SIZE; return 0;
}
rc = ecryptfs_get_lower_file(dentry, inode); if (rc) return rc;
crypt_stat = &ecryptfs_inode_to_private(d_inode(dentry))->crypt_stat; /* Switch on growing or shrinking file */ if (ia->ia_size > i_size) { char zero[] = { 0x00 };
lower_ia->ia_valid &= ~ATTR_SIZE; /* Write a single 0 at the last position of the file; * this triggers code that will fill in 0's throughout * the intermediate portion of the previous end of the
* file and the new and of the file */
rc = ecryptfs_write(inode, zero,
(ia->ia_size - 1), 1);
} else { /* ia->ia_size < i_size_read(inode) */ /* We're chopping off all the pages down to the page * in which ia->ia_size is located. Fill in the end of * that page from (ia->ia_size & ~PAGE_MASK) to
* PAGE_SIZE with zeros. */
size_t num_zeros = (PAGE_SIZE
- (ia->ia_size & ~PAGE_MASK));
zeros_virt = kzalloc(num_zeros, GFP_KERNEL); if (!zeros_virt) {
rc = -ENOMEM; goto out;
}
rc = ecryptfs_write(inode, zeros_virt,
ia->ia_size, num_zeros);
kfree(zeros_virt); if (rc) {
printk(KERN_ERR "Error attempting to zero out " "the remainder of the end page on " "reducing truncate; rc = [%d]\n", rc); goto out;
}
}
truncate_setsize(inode, ia->ia_size);
rc = ecryptfs_write_inode_size_to_metadata(inode); if (rc) {
printk(KERN_ERR "Problem with " "ecryptfs_write_inode_size_to_metadata; " "rc = [%d]\n", rc); goto out;
} /* We are reducing the size of the ecryptfs file, and need to
* know if we need to reduce the size of the lower file. */
lower_size_before_truncate =
upper_size_to_lower_size(crypt_stat, i_size);
lower_size_after_truncate =
upper_size_to_lower_size(crypt_stat, ia->ia_size); if (lower_size_after_truncate < lower_size_before_truncate) {
lower_ia->ia_size = lower_size_after_truncate;
lower_ia->ia_valid |= ATTR_SIZE;
} else
lower_ia->ia_valid &= ~ATTR_SIZE;
}
out:
ecryptfs_put_lower_file(inode); return rc;
}
crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
lower_oldsize = upper_size_to_lower_size(crypt_stat,
i_size_read(inode));
lower_newsize = upper_size_to_lower_size(crypt_stat, offset); if (lower_newsize > lower_oldsize) { /* * The eCryptfs inode and the new *lower* size are mixed here * because we may not have the lower i_mutex held and/or it may * not be appropriate to call inode_newsize_ok() with inodes * from other filesystems.
*/ return inode_newsize_ok(inode, lower_newsize);
}
return 0;
}
/** * ecryptfs_truncate * @dentry: The ecryptfs layer dentry * @new_length: The length to expand the file to * * Simple function that handles the truncation of an eCryptfs inode and * its corresponding lower inode. * * Returns zero on success; non-zero otherwise
*/ int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
{ struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length }; struct iattr lower_ia = { .ia_valid = 0 }; int rc;
rc = ecryptfs_inode_newsize_ok(d_inode(dentry), new_length); if (rc) return rc;
/** * ecryptfs_setattr * @idmap: idmap of the target mount * @dentry: dentry handle to the inode to modify * @ia: Structure with flags of what to change and values * * Updates the metadata of an inode. If the update is to the size * i.e. truncation, then ecryptfs_truncate will handle the size modification * of both the ecryptfs inode and the lower inode. * * All other metadata changes will be passed right to the lower filesystem, * and we will just update our inode to look like the lower.
*/ staticint ecryptfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct iattr *ia)
{ int rc = 0; struct dentry *lower_dentry; struct iattr lower_ia; struct inode *inode; struct inode *lower_inode; struct ecryptfs_crypt_stat *crypt_stat;
mount_crypt_stat = &ecryptfs_superblock_to_private(
dentry->d_sb)->mount_crypt_stat;
rc = ecryptfs_get_lower_file(dentry, inode); if (rc) {
mutex_unlock(&crypt_stat->cs_mutex); goto out;
}
rc = ecryptfs_read_metadata(dentry);
ecryptfs_put_lower_file(inode); if (rc) { if (!(mount_crypt_stat->flags
& ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
rc = -EIO;
printk(KERN_WARNING "Either the lower file " "is not in a valid eCryptfs format, " "or the key could not be retrieved. " "Plaintext passthrough mode is not " "enabled; returning -EIO\n");
mutex_unlock(&crypt_stat->cs_mutex); goto out;
}
rc = 0;
crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
| ECRYPTFS_ENCRYPTED);
}
}
mutex_unlock(&crypt_stat->cs_mutex);
rc = setattr_prepare(&nop_mnt_idmap, dentry, ia); if (rc) goto out; if (ia->ia_valid & ATTR_SIZE) {
rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size); if (rc) goto out;
}
memcpy(&lower_ia, ia, sizeof(lower_ia)); if (ia->ia_valid & ATTR_FILE)
lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file); if (ia->ia_valid & ATTR_SIZE) {
rc = truncate_upper(dentry, ia, &lower_ia); if (rc < 0) goto out;
}
/* * mode change is for clearing setuid/setgid bits. Allow lower fs * to interpret this in its own way.
*/ if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
lower_ia.ia_valid &= ~ATTR_MODE;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.