Skip to contentSkip to conversation

truncate(2) and setattr

DISCUSSION Russell Coker 2 participants 5 messages Apr 30, 2003 2 messages not in the archive SELinux security module

lore
Share & tools
lore
#0 lore raw

Why does truncate(2) require setattr access to the file?

Using truncate(file, len + 1) to increase the size of the file by 1 byte has exactly the same affect on the file attributes as seeking to the end and writing one byte.

Using truncate to make a file smaller has no greater affect on the file attributes.

Signature · 10 lines
http://www.coker.com.au/selinux/   My NSA Security Enhanced Linux packages
http://www.coker.com.au/bonnie++/  Bonnie++ hard drive benchmark
http://www.coker.com.au/postal/    Postal SMTP/POP benchmark
http://www.coker.com.au/~russell/  My home page


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#1 lore raw
Quoted 8 lines · Russell Coker

Why does truncate(2) require setattr access to the file?

Using truncate(file, len + 1) to increase the size of the file by 1 byte has exactly the same affect on the file attributes as seeking to the end and writing one byte.

Using truncate to make a file smaller has no greater affect on the file attributes.

The size of a file is one of its attributes. If you look at the internal implementation in fs/open.c:do_truncate, you'll see that it calls fs/attr.c:notify_change with flags indicating that the size and ctime attributes are changing. notify_change is also used for other attribute changes, e.g. chown, chmod, utimes. If you look at fs/attr.c:notify_change, you'll see that it calls the security_inode_setattr hook to perform a permission check prior to calling the inode->i_op->setattr operation or the inode_setattr function. In SELinux, the corresponding hook function (security/selinux/hooks.c:selinux_inode_setattr) always checks setattr permission to the file.

Now, it is also true that fs/open.c:do_sys_truncate checks write permission to the inode by calling fs/namei.c:permission, which calls the security_inode_permission hook == selinux_inode_permission for SELinux, and fs/open.c:do_sys_ftruncate requires that the file mode includes write access. Hence, at present, both write permission and setattr permission are required for truncate. This reflects the fact that truncate affects both the file data and its size attribute.

A case could be made for splitting up the SELinux setattr file permission into distinct permissions for the different attributes, and checking the appropriate permission in selinux_inode_setattr based on the iattr->ia_valid flags, similar to how fs/attr.c:inode_change_ok performs different checks depending on those flags. This is a fine line in terms of balancing between providing least privilege and making the policy unnecessarily complicated, as SELinux itself does not depend on the values of these attributes for its own access controls.

Signature · 8 lines
Stephen Smalley <[email protected]>
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#4 lore raw
In reply to a message not in the archive
Quoted 4 lines · Russell Coker

This issue was first raised on the SE Linux IRC channel. It was pointed out that rw_file_perms does not permit truncating a file, which is not the expected behavior.

Quoted 8 lines

I don't think that changing this would require any additional complexity in the policy. IMHO truncate() access should be determined by the write permission (and possibly append permission for truncate() being used to increase the size of a file).

I suspect that the end result of such a change is that we would be able to remove many instances of setattr permission from various domains that only really need truncate() access.

So you want something like the below patch, which omits the file setattr permission check in the truncate case.

Index: lsm-2.4/security/selinux/hooks.c
===================================================================
RCS file: /home/pal/CVS/lsm-2.4/security/selinux/hooks.c,v retrieving revision 1.89

security/selinux/hooks.c +4 -0
diff -u -r1.89 hooks.c
@@ -2034,6 +2034,10 @@
 
 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
 {
+	if (iattr->ia_valid == (ATTR_SIZE | ATTR_CTIME)) {
+		/* truncate covered by write permission check */
+		return 0;
+	}
 	return dentry_has_perm(current, dentry, FILE__SETATTR, NULL);
 }
 
Signature · 8 lines
Stephen Smalley <[email protected]>
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#5 lore raw
Quoted 2 lines · Stephen Smalley

So you want something like the below patch, which omits the file setattr permission check in the truncate case.

Yes. I believe that using write permission checks for this is the appropriate thing to do. I think that a CTIME change does not deserve much attention, and size is a direct function of write/append.

Quoted 17 lines

Index: lsm-2.4/security/selinux/hooks.c
===================================================================
RCS file: /home/pal/CVS/lsm-2.4/security/selinux/hooks.c,v retrieving revision 1.89
diff -u -r1.89 hooks.c
--- lsm-2.4/security/selinux/hooks.c 27 Mar 2003 20:05:18
-0000 1.89 +++ lsm-2.4/security/selinux/hooks.c 30 Apr 2003
13:09:39 -0000 @@ -2034,6 +2034,10 @@

static int selinux_inode_setattr(struct dentry *dentry, struct iattr
*iattr) {

security/selinux/hooks.c +4 -0
+       if (iattr->ia_valid == (ATTR_SIZE | ATTR_CTIME)) {
+               /* truncate covered by write permission check */
+               return 0;
+       }
        return dentry_has_perm(current, dentry, FILE__SETATTR, NULL);
 }
Signature · 10 lines
http://www.coker.com.au/selinux/   My NSA Security Enhanced Linux packages
http://www.coker.com.au/bonnie++/  Bonnie++ hard drive benchmark
http://www.coker.com.au/postal/    Postal SMTP/POP benchmark
http://www.coker.com.au/~russell/  My home page


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
#6 lore raw
Quoted 3 lines · Russell Coker

Yes. I believe that using write permission checks for this is the appropriate thing to do. I think that a CTIME change does not deserve much attention, and size is a direct function of write/append.

truncate sets the ATTR_SIZE and ATTR_CTIME flags before calling notify_change; that is why ATTR_CTIME was included in the test. However, in thinking about this further, I think it is lacking in two
respects: 1) We should recheck write permission in selinux_inode_setattr in the truncate case to ensure revalidation in the ftruncate case (e.g. in case the file was relabeled or the policy was changed after the open), and 2) We should handle utimes(p,NULL) in the same manner, only checking write permission for it, as it merely sets the timestamps to the current time. For changes to the mode, uid, gid, or explicit setting of timestamps via utimes(p,&times), we should still check setattr permission. We could split the utimes(p,&times) case into a separate permission to distinguish it from changes to the DAC protection state of the file, but this will require adding a new permission to the file class. The patch below does everything except split utimes(p,&times) into a separate permission:

Index: lsm-2.4/security/selinux/hooks.c
===================================================================
RCS file: /home/pal/CVS/lsm-2.4/security/selinux/hooks.c,v retrieving revision 1.89

security/selinux/hooks.c +5 -1
diff -u -r1.89 hooks.c
@@ -2034,7 +2034,11 @@
 
 static int selinux_inode_setattr(struct dentry *dentry, struct iattr *iattr)
 {
-	return dentry_has_perm(current, dentry, FILE__SETATTR, NULL);
+	if (iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | 
+			       ATTR_ATIME_SET | ATTR_MTIME_SET))
+		return dentry_has_perm(current, dentry, FILE__SETATTR, NULL);
+
+	return dentry_has_perm(current, dentry, FILE__WRITE, NULL);
 }
 
 static int selinux_inode_stat(struct inode *inode)
Signature · 8 lines
Stephen Smalley <[email protected]>
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.