import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ubifs / xattr.c
index 0f7139bdb2c20370f7f81489b14db155128c2f62..86e6f78118af6c449a7a01fa836083bd0b21f5e7 100644 (file)
@@ -100,24 +100,30 @@ static const struct file_operations empty_fops;
 static int create_xattr(struct ubifs_info *c, struct inode *host,
                        const struct qstr *nm, const void *value, int size)
 {
-       int err;
+       int err, names_len;
        struct inode *inode;
        struct ubifs_inode *ui, *host_ui = ubifs_inode(host);
        struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
                                .new_ino_d = ALIGN(size, 8), .dirtied_ino = 1,
                                .dirtied_ino_d = ALIGN(host_ui->data_len, 8) };
 
-       if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE)
+       if (host_ui->xattr_cnt >= MAX_XATTRS_PER_INODE) {
+               ubifs_err("inode %lu already has too many xattrs (%d), cannot create more",
+                         host->i_ino, host_ui->xattr_cnt);
                return -ENOSPC;
+       }
        /*
         * Linux limits the maximum size of the extended attribute names list
         * to %XATTR_LIST_MAX. This means we should not allow creating more
         * extended attributes if the name list becomes larger. This limitation
         * is artificial for UBIFS, though.
         */
-       if (host_ui->xattr_names + host_ui->xattr_cnt +
-                                       nm->len + 1 > XATTR_LIST_MAX)
+       names_len = host_ui->xattr_names + host_ui->xattr_cnt + nm->len + 1;
+       if (names_len > XATTR_LIST_MAX) {
+               ubifs_err("cannot add one more xattr name to inode %lu, total names length would become %d, max. is %d",
+                         host->i_ino, names_len, XATTR_LIST_MAX);
                return -ENOSPC;
+       }
 
        err = ubifs_budget_space(c, &req);
        if (err)
@@ -293,18 +299,16 @@ static struct inode *iget_xattr(struct ubifs_info *c, ino_t inum)
        return ERR_PTR(-EINVAL);
 }
 
-int ubifs_setxattr(struct dentry *dentry, const char *name,
-                  const void *value, size_t size, int flags)
+static int setxattr(struct inode *host, const char *name, const void *value,
+                   size_t size, int flags)
 {
-       struct inode *inode, *host = dentry->d_inode;
+       struct inode *inode;
        struct ubifs_info *c = host->i_sb->s_fs_info;
        struct qstr nm = QSTR_INIT(name, strlen(name));
        struct ubifs_dent_node *xent;
        union ubifs_key key;
        int err, type;
 
-       dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
-               host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
        ubifs_assert(mutex_is_locked(&host->i_mutex));
 
        if (size > UBIFS_MAX_INO_DATA)
@@ -356,8 +360,18 @@ out_free:
        return err;
 }
 
+int ubifs_setxattr(struct dentry *dentry, const char *name,
+                          const void *value, size_t size, int flags)
+{
+       struct inode *host = dentry->d_inode;
+       dbg_gen("xattr '%s', host ino %lu ('%.*s'), size %zd", name,
+               host->i_ino, dentry->d_name.len, dentry->d_name.name, size);
+
+       return setxattr(dentry->d_inode, name, value, size, flags);
+}
+
 ssize_t ubifs_getxattr(struct dentry *dentry, const char *name, void *buf,
-                      size_t size)
+        size_t size)
 {
        struct inode *inode, *host = dentry->d_inode;
        struct ubifs_info *c = host->i_sb->s_fs_info;
@@ -568,3 +582,86 @@ out_free:
        kfree(xent);
        return err;
 }
+
+static size_t security_listxattr(struct dentry *d, char *list, size_t list_size,
+               const char *name, size_t name_len, int flags)
+{
+       const int prefix_len = XATTR_SECURITY_PREFIX_LEN;
+       const size_t total_len = prefix_len + name_len + 1;
+       if (list && total_len <= list_size) {
+               memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
+               memcpy(list+prefix_len, name, name_len);
+               list[prefix_len + name_len] = '\0';
+       }
+       return total_len;
+}
+
+static int security_getxattr(struct dentry *d, const char *name, void *buffer,
+                     size_t size, int flags)
+{
+       return ubifs_getxattr(d, name, buffer, size);
+}
+
+static int security_setxattr(struct dentry *d, const char *name,
+                            const void *value, size_t size, int flags,
+                            int handler_flags)
+{
+       return ubifs_setxattr(d, name, value, size, flags);
+}
+
+static const struct xattr_handler ubifs_xattr_security_handler = {
+       .prefix = XATTR_SECURITY_PREFIX,
+       .list   = security_listxattr,
+       .get    = security_getxattr,
+       .set    = security_setxattr,
+};
+
+const struct xattr_handler *ubifs_xattr_handlers[] = {
+       &ubifs_xattr_security_handler,
+       NULL,
+};
+
+static int init_xattrs(struct inode *inode, const struct xattr *xattr_array,
+                     void *fs_info)
+{
+       const struct xattr *xattr;
+       char *name;
+       int err = 0;
+
+       for (xattr = xattr_array; xattr->name != NULL; xattr++) {
+               name = kmalloc(XATTR_SECURITY_PREFIX_LEN +
+                               strlen(xattr->name) + 1, GFP_NOFS);
+               if (!name) {
+                       err = -ENOMEM;
+                       break;
+               }
+               strcpy(name, XATTR_SECURITY_PREFIX);
+               strcpy(name + XATTR_SECURITY_PREFIX_LEN, xattr->name);
+               err = setxattr(inode, name, xattr->value, xattr->value_len, 0);
+               kfree(name);
+               if (err < 0)
+                       break;
+       }
+       return err;
+}
+
+int ubifs_init_security(struct inode *dentry, struct inode *inode,
+               const struct qstr *qstr)
+{
+       int err;
+       /*
+        *FIXME:Ugly hack. disable possible lockdep as it detects possible 
+        *deadlock
+        */
+       lockdep_off();
+       mutex_lock(&inode->i_mutex);
+       err = security_inode_init_security(inode, dentry, qstr,
+                                          &init_xattrs, 0);
+       mutex_unlock(&inode->i_mutex);
+       lockdep_on();
+
+       if (err)
+               ubifs_err("cannot initialize security for inode %lu, error %d",
+                         inode->i_ino, err);
+       return err;
+}