Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include "reiserfs.h"
0003 #include <linux/errno.h>
0004 #include <linux/fs.h>
0005 #include <linux/pagemap.h>
0006 #include <linux/xattr.h>
0007 #include <linux/slab.h>
0008 #include "xattr.h"
0009 #include <linux/security.h>
0010 #include <linux/uaccess.h>
0011 
0012 static int
0013 security_get(const struct xattr_handler *handler, struct dentry *unused,
0014          struct inode *inode, const char *name, void *buffer, size_t size)
0015 {
0016     if (IS_PRIVATE(inode))
0017         return -EPERM;
0018 
0019     return reiserfs_xattr_get(inode, xattr_full_name(handler, name),
0020                   buffer, size);
0021 }
0022 
0023 static int
0024 security_set(const struct xattr_handler *handler,
0025          struct user_namespace *mnt_userns, struct dentry *unused,
0026          struct inode *inode, const char *name, const void *buffer,
0027          size_t size, int flags)
0028 {
0029     if (IS_PRIVATE(inode))
0030         return -EPERM;
0031 
0032     return reiserfs_xattr_set(inode,
0033                   xattr_full_name(handler, name),
0034                   buffer, size, flags);
0035 }
0036 
0037 static bool security_list(struct dentry *dentry)
0038 {
0039     return !IS_PRIVATE(d_inode(dentry));
0040 }
0041 
0042 /* Initializes the security context for a new inode and returns the number
0043  * of blocks needed for the transaction. If successful, reiserfs_security
0044  * must be released using reiserfs_security_free when the caller is done. */
0045 int reiserfs_security_init(struct inode *dir, struct inode *inode,
0046                const struct qstr *qstr,
0047                struct reiserfs_security_handle *sec)
0048 {
0049     int blocks = 0;
0050     int error;
0051 
0052     sec->name = NULL;
0053 
0054     /* Don't add selinux attributes on xattrs - they'll never get used */
0055     if (IS_PRIVATE(dir))
0056         return 0;
0057 
0058     error = security_old_inode_init_security(inode, dir, qstr, &sec->name,
0059                          &sec->value, &sec->length);
0060     if (error) {
0061         if (error == -EOPNOTSUPP)
0062             error = 0;
0063 
0064         sec->name = NULL;
0065         sec->value = NULL;
0066         sec->length = 0;
0067         return error;
0068     }
0069 
0070     if (sec->length && reiserfs_xattrs_initialized(inode->i_sb)) {
0071         blocks = reiserfs_xattr_jcreate_nblocks(inode) +
0072              reiserfs_xattr_nblocks(inode, sec->length);
0073         /* We don't want to count the directories twice if we have
0074          * a default ACL. */
0075         REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
0076     }
0077     return blocks;
0078 }
0079 
0080 int reiserfs_security_write(struct reiserfs_transaction_handle *th,
0081                 struct inode *inode,
0082                 struct reiserfs_security_handle *sec)
0083 {
0084     int error;
0085     if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
0086         return -EINVAL;
0087 
0088     error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
0089                       sec->length, XATTR_CREATE);
0090     if (error == -ENODATA || error == -EOPNOTSUPP)
0091         error = 0;
0092 
0093     return error;
0094 }
0095 
0096 void reiserfs_security_free(struct reiserfs_security_handle *sec)
0097 {
0098     kfree(sec->name);
0099     kfree(sec->value);
0100     sec->name = NULL;
0101     sec->value = NULL;
0102 }
0103 
0104 const struct xattr_handler reiserfs_xattr_security_handler = {
0105     .prefix = XATTR_SECURITY_PREFIX,
0106     .get = security_get,
0107     .set = security_set,
0108     .list = security_list,
0109 };