cb8e9e
From b1de5fd8f480bb6cd034242c49a1f85ff6c21e51 Mon Sep 17 00:00:00 2001
cb8e9e
From: Raghavendra Bhat <raghavendra@redhat.com>
cb8e9e
Date: Wed, 24 Jun 2015 20:06:01 +0530
cb8e9e
Subject: [PATCH 186/190] features/bit-rot-stub: do not allow setxattr and removexattr on bit-rot xattrs
cb8e9e
cb8e9e
        Backport of http://review.gluster.org/11389
cb8e9e
cb8e9e
* setxattr and {f}removexattr of versioning, signature and bad-file xattrs are
cb8e9e
  returned with error.
cb8e9e
cb8e9e
Change-Id: I21b25e06b6b78309c0ddd155345fec8765e03352
cb8e9e
BUG: 1224227
cb8e9e
Reviewed-on: https://code.engineering.redhat.com/gerrit/51758
cb8e9e
Reviewed-by: Venky Shankar <vshankar@redhat.com>
cb8e9e
Tested-by: Venky Shankar <vshankar@redhat.com>
cb8e9e
---
cb8e9e
 .../bit-rot/src/stub/bit-rot-stub-messages.h       |   10 ++-
cb8e9e
 xlators/features/bit-rot/src/stub/bit-rot-stub.c   |  103 ++++++++++++++++++-
cb8e9e
 2 files changed, 106 insertions(+), 7 deletions(-)
cb8e9e
cb8e9e
diff --git a/xlators/features/bit-rot/src/stub/bit-rot-stub-messages.h b/xlators/features/bit-rot/src/stub/bit-rot-stub-messages.h
cb8e9e
index db5736a..532c2be 100644
cb8e9e
--- a/xlators/features/bit-rot/src/stub/bit-rot-stub-messages.h
cb8e9e
+++ b/xlators/features/bit-rot/src/stub/bit-rot-stub-messages.h
cb8e9e
@@ -167,7 +167,14 @@
cb8e9e
  * @recommendedaction
cb8e9e
  *
cb8e9e
  */
cb8e9e
-#define BRS_MSG_REMOVE_BAD_OBJECT_XATTR     (GLFS_BITROT_STUB_BASE + 18)
cb8e9e
+#define BRS_MSG_REMOVE_INTERNAL_XATTR       (GLFS_BITROT_STUB_BASE + 18)
cb8e9e
+/*!
cb8e9e
+ * @messageid
cb8e9e
+ * @diagnosis
cb8e9e
+ * @recommendedaction
cb8e9e
+ *
cb8e9e
+ */
cb8e9e
+#define BRS_MSG_SET_INTERNAL_XATTR          (GLFS_BITROT_STUB_BASE + 19)
cb8e9e
 /*!
cb8e9e
  * @messageid
cb8e9e
  * @diagnosis
cb8e9e
@@ -182,5 +189,6 @@
cb8e9e
  *
cb8e9e
  */
cb8e9e
 /*------------*/
cb8e9e
+
cb8e9e
 #define glfs_msg_end_x GLFS_MSGID_END, "Invalid: End of messages"
cb8e9e
 #endif /* !_BITROT_STUB_MESSAGES_H_ */
cb8e9e
diff --git a/xlators/features/bit-rot/src/stub/bit-rot-stub.c b/xlators/features/bit-rot/src/stub/bit-rot-stub.c
cb8e9e
index de81510..41c8359 100644
cb8e9e
--- a/xlators/features/bit-rot/src/stub/bit-rot-stub.c
cb8e9e
+++ b/xlators/features/bit-rot/src/stub/bit-rot-stub.c
cb8e9e
@@ -993,6 +993,37 @@ unwind:
cb8e9e
         return 0;
cb8e9e
 }
cb8e9e
 
cb8e9e
+
cb8e9e
+/**
cb8e9e
+ * As of now, versioning is done by the stub (though as a setxattr
cb8e9e
+ * operation) as part of inode modification operations such as writev,
cb8e9e
+ * truncate, ftruncate. And signing is done by BitD by a fsetxattr call.
cb8e9e
+ * So any kind of setxattr coming on the versioning and the signing xattr is
cb8e9e
+ * not allowed (i.e. BITROT_CURRENT_VERSION_KEY and BITROT_SIGNING_VERSION_KEY).
cb8e9e
+ * In future if BitD/scrubber are allowed to change the versioning
cb8e9e
+ * xattrs (though I cannot see a reason for it as of now), then the below
cb8e9e
+ * function can be modified to block setxattr on version for only applications.
cb8e9e
+ *
cb8e9e
+ * NOTE: BitD sends sign request on GLUSTERFS_SET_OBJECT_SIGNATURE key.
cb8e9e
+ *       BITROT_SIGNING_VERSION_KEY is the xattr used to save the signature.
cb8e9e
+ *
cb8e9e
+ */
cb8e9e
+static int32_t
cb8e9e
+br_stub_handle_internal_xattr (call_frame_t *frame, xlator_t *this, fd_t *fd,
cb8e9e
+                               char *key)
cb8e9e
+{
cb8e9e
+        int32_t          op_ret   = -1;
cb8e9e
+        int32_t          op_errno = EINVAL;
cb8e9e
+
cb8e9e
+        gf_msg (this->name, GF_LOG_ERROR, 0,
cb8e9e
+                BRS_MSG_SET_INTERNAL_XATTR, "setxattr called"
cb8e9e
+                " on the internal xattr %s for inode %s", key,
cb8e9e
+                uuid_utoa (fd->inode->gfid));
cb8e9e
+
cb8e9e
+        STACK_UNWIND_STRICT (fsetxattr, frame, op_ret, op_errno, NULL);
cb8e9e
+        return 0;
cb8e9e
+}
cb8e9e
+
cb8e9e
 int
cb8e9e
 br_stub_fsetxattr (call_frame_t *frame, xlator_t *this,
cb8e9e
                    fd_t *fd, dict_t *dict, int flags, dict_t *xdata)
cb8e9e
@@ -1013,6 +1044,20 @@ br_stub_fsetxattr (call_frame_t *frame, xlator_t *this,
cb8e9e
                 goto done;
cb8e9e
         }
cb8e9e
 
cb8e9e
+        /* signing xattr */
cb8e9e
+        if (dict_get(dict, BITROT_SIGNING_VERSION_KEY)) {
cb8e9e
+                br_stub_handle_internal_xattr (frame, this, fd,
cb8e9e
+                                               BITROT_SIGNING_VERSION_KEY);
cb8e9e
+                goto done;
cb8e9e
+        }
cb8e9e
+
cb8e9e
+        /* version xattr */
cb8e9e
+        if (dict_get(dict, BITROT_CURRENT_VERSION_KEY)) {
cb8e9e
+                br_stub_handle_internal_xattr (frame, this, fd,
cb8e9e
+                                               BITROT_CURRENT_VERSION_KEY);
cb8e9e
+                goto done;
cb8e9e
+        }
cb8e9e
+
cb8e9e
         /* object reopen request */
cb8e9e
         ret = dict_get_uint32 (dict, BR_REOPEN_SIGN_HINT_KEY, &val;;
cb8e9e
         if (!ret) {
cb8e9e
@@ -1035,6 +1080,45 @@ done:
cb8e9e
         return 0;
cb8e9e
 }
cb8e9e
 
cb8e9e
+
cb8e9e
+/**
cb8e9e
+ * Currently BitD and scrubber are doing fsetxattr to either sign the object
cb8e9e
+ * or to mark it as bad. Hence setxattr on any of those keys is denied directly
cb8e9e
+ * without checking from where the fop is coming.
cb8e9e
+ * Later, if BitD or Scrubber does setxattr of those keys, then appropriate
cb8e9e
+ * check has to be added below.
cb8e9e
+ */
cb8e9e
+int
cb8e9e
+br_stub_setxattr (call_frame_t *frame, xlator_t *this,
cb8e9e
+                  loc_t *loc, dict_t *dict, int flags, dict_t *xdata)
cb8e9e
+{
cb8e9e
+        int32_t  op_ret                    = -1;
cb8e9e
+        int32_t  op_errno                  = EINVAL;
cb8e9e
+        char     dump[64*1024]             = {0,};
cb8e9e
+        char    *format                    = "(%s:%s)";
cb8e9e
+
cb8e9e
+        if (dict_get (dict, GLUSTERFS_SET_OBJECT_SIGNATURE) ||
cb8e9e
+            dict_get (dict, BR_REOPEN_SIGN_HINT_KEY) ||
cb8e9e
+            dict_get (dict, BITROT_OBJECT_BAD_KEY) ||
cb8e9e
+            dict_get (dict, BITROT_SIGNING_VERSION_KEY) ||
cb8e9e
+            dict_get (dict, BITROT_CURRENT_VERSION_KEY)) {
cb8e9e
+                dict_dump_to_str (dict, dump, sizeof(dump), format);
cb8e9e
+                gf_msg (this->name, GF_LOG_ERROR, 0,
cb8e9e
+                        BRS_MSG_SET_INTERNAL_XATTR, "setxattr called on "
cb8e9e
+                        "internal xattr %s", dump);
cb8e9e
+                goto unwind;
cb8e9e
+        }
cb8e9e
+
cb8e9e
+
cb8e9e
+        STACK_WIND_TAIL (frame, FIRST_CHILD (this),
cb8e9e
+                         FIRST_CHILD (this)->fops->setxattr, loc, dict, flags,
cb8e9e
+                         xdata);
cb8e9e
+        return 0;
cb8e9e
+unwind:
cb8e9e
+        STACK_UNWIND_STRICT (setxattr, frame, op_ret, op_errno, NULL);
cb8e9e
+        return 0;
cb8e9e
+}
cb8e9e
+
cb8e9e
 /** }}} */
cb8e9e
 
cb8e9e
 
cb8e9e
@@ -1049,13 +1133,16 @@ br_stub_removexattr (call_frame_t *frame, xlator_t *this,
cb8e9e
         int32_t op_ret    = -1;
cb8e9e
         int32_t op_errno  = EINVAL;
cb8e9e
 
cb8e9e
-        if (!strcmp (BITROT_OBJECT_BAD_KEY, name)) {
cb8e9e
+        if (!strcmp (BITROT_OBJECT_BAD_KEY, name) ||
cb8e9e
+            !strcmp (BITROT_SIGNING_VERSION_KEY, name) ||
cb8e9e
+            !strcmp (BITROT_CURRENT_VERSION_KEY, name)) {
cb8e9e
                 gf_msg (this->name, GF_LOG_WARNING, 0,
cb8e9e
-                        BRS_MSG_REMOVE_BAD_OBJECT_XATTR, "Remove xattr called"
cb8e9e
-                        " on bad object xattr for file %s", loc->path);
cb8e9e
+                        BRS_MSG_REMOVE_INTERNAL_XATTR, "removexattr called"
cb8e9e
+                        " on internal xattr %s for file %s", name, loc->path);
cb8e9e
                 goto unwind;
cb8e9e
         }
cb8e9e
 
cb8e9e
+
cb8e9e
         STACK_WIND_TAIL (frame, FIRST_CHILD(this),
cb8e9e
                          FIRST_CHILD(this)->fops->removexattr,
cb8e9e
                          loc, name, xdata);
cb8e9e
@@ -1072,14 +1159,17 @@ br_stub_fremovexattr (call_frame_t *frame, xlator_t *this,
cb8e9e
         int32_t op_ret    = -1;
cb8e9e
         int32_t op_errno  = EINVAL;
cb8e9e
 
cb8e9e
-        if (!strcmp (BITROT_OBJECT_BAD_KEY, name)) {
cb8e9e
+        if (!strcmp (BITROT_OBJECT_BAD_KEY, name) ||
cb8e9e
+            !strcmp (BITROT_SIGNING_VERSION_KEY, name) ||
cb8e9e
+            !strcmp (BITROT_CURRENT_VERSION_KEY, name)) {
cb8e9e
                 gf_msg (this->name, GF_LOG_WARNING, 0,
cb8e9e
-                        BRS_MSG_REMOVE_BAD_OBJECT_XATTR, "Remove xattr called"
cb8e9e
-                        " on bad object xattr for inode %s",
cb8e9e
+                        BRS_MSG_REMOVE_INTERNAL_XATTR, "removexattr called"
cb8e9e
+                        " on internal xattr %s for inode %s", name,
cb8e9e
                         uuid_utoa (fd->inode->gfid));
cb8e9e
                 goto unwind;
cb8e9e
         }
cb8e9e
 
cb8e9e
+
cb8e9e
         STACK_WIND_TAIL (frame, FIRST_CHILD(this),
cb8e9e
                          FIRST_CHILD(this)->fops->fremovexattr,
cb8e9e
                          fd, name, xdata);
cb8e9e
@@ -2593,6 +2683,7 @@ struct xlator_fops fops = {
cb8e9e
         .readv     = br_stub_readv,
cb8e9e
         .removexattr = br_stub_removexattr,
cb8e9e
         .fremovexattr = br_stub_fremovexattr,
cb8e9e
+        .setxattr  = br_stub_setxattr,
cb8e9e
 };
cb8e9e
 
cb8e9e
 struct xlator_cbks cbks = {
cb8e9e
-- 
cb8e9e
1.7.1
cb8e9e