Blob Blame History Raw
From b6ebd172912d298223990487f14460faf3e959df Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Tue, 20 Jun 2017 15:21:07 -0400
Subject: [PATCH 2/4] cadecoder: only try to set settable chattr bits
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

It seems that after all, FS_IOC_GETFLAGS should take an int*.
It does not set the high bits in the long argument. By explicitly
initalizing it to 0, we avoid garbage there. I'm not sure how this
will play out on a big-endian system…

Independently, we shouldn't be trying to set bits like FS_EXTENT_FL,
which chattr(1) says "cannot be set by chattr", i.e. presumably also
not by us.
---
 src/cadecoder.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/cadecoder.c b/src/cadecoder.c
index d3167348fa..d65938e839 100644
--- a/src/cadecoder.c
+++ b/src/cadecoder.c
@@ -3642,18 +3642,22 @@ static int ca_decoder_finalize_child(CaDecoder *d, CaDecoderNode *n, CaDecoderNo
         }
 
         if ((d->feature_flags & CA_FORMAT_WITH_CHATTR) != 0 && child->fd >= 0) {
-                long new_attr, old_attr;
+                long new_attr,
+                     old_attr = 0; /* Initialize, 'cuz ioctl() does not set the high bits. */;
 
                 new_attr = ca_feature_flags_to_chattr(read_le64(&child->entry->flags) & d->feature_flags);
+                assert((new_attr & ~FS_FL_USER_MODIFIABLE) == 0);
 
                 if (ioctl(child->fd, FS_IOC_GETFLAGS, &old_attr) < 0) {
 
                         if (new_attr != 0 || !IN_SET(errno, ENOTTY, ENOSYS, EBADF, EOPNOTSUPP))
                                 return -errno;
 
-                } else if (old_attr != new_attr) {
+                } else if ((old_attr & FS_FL_USER_MODIFIABLE) != new_attr) {
+                        long final_attr;
 
-                        if (ioctl(child->fd, FS_IOC_SETFLAGS, &new_attr) < 0)
+                        final_attr = (old_attr & !FS_FL_USER_MODIFIABLE) | new_attr;
+                        if (ioctl(child->fd, FS_IOC_SETFLAGS, &final_attr) < 0)
                                 return -errno;
                 }
         }
-- 
2.13.0