diff mbox

Add file permission mode helpers

Message ID 20160803081140.GA7833@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ingo Molnar Aug. 3, 2016, 8:11 a.m. UTC
* Linus Torvalds <torvalds@linux-foundation.org> wrote:

> [ So I answered similarly to another patch, but I'll just re-iterate
> and change the subject line so that it stands out a bit from the
> millions of actual patches ]
> 
> On Tue, Aug 2, 2016 at 1:42 PM, Pavel Machek <pavel@ucw.cz> wrote:
> >
> > Everyone knows what 0644 is, but noone can read S_IRUSR | S_IWUSR |
> > S_IRCRP | S_IROTH (*). Please don't do this.
> 
> Absolutely. It's *much* easier to parse and understand the octal
> numbers, while the symbolic macro names are just random line noise and
> hard as hell to understand. You really have to think about it.
> 
> So we should rather go the other way: convert existing bad symbolic
> permission bit macro use to just use the octal numbers.

In addition to that I'd love to have something even easier to read, a few common 
variants of the permissions field of 'ls -l' pre-defined. I did some quick 
grepping, and collected the main variants that are in use:

		PERM_r________	0400
		PERM_r__r_____	0440
		PERM_r__r__r__	0444

		PERM_rw_______	0600
		PERM_rw_r_____	0640
		PERM_rw_r__r__	0644
		PERM_rw_rw_r__	0664
		PERM_rw_rw_rw_	0666

		PERM__w_______	0200
		PERM__w__w____	0220
		PERM__w__w__w_	0222

		PERM_r_x______	0500
		PERM_r_xr_x___	0550
		PERM_r_xr_xr_x	0555

		PERM_rwx______	0700
		PERM_rwxr_x___	0750
		PERM_rwxr_xr_x	0755
		PERM_rwxrwxr_x	0775
		PERM_rwxrwxrwx	0777

		PERM__wx______	0300
		PERM__wx_wx___	0330
		PERM__wx_wx_wx	0333

Allowing these would be nice too, because there were cases in the past where 
people messed up the octal representation or our internal symbolic helpers,
but this representation is fundamentally self-describing and pretty 'fool proof'.

An added advantage would be that during review it would stick out like a sore 
thumb if anyone used a 'weird' permission variant.

For example, if you saw these lines in a driver patch:

+	__ATTR(l1, 0444, driver_show_l4, NULL);
+		__ATTR(l3, 0446, driver_show_l4, NULL);
+			__ATTR(l2, 04444, driver_show_l4, NULL);
+		__ATTR(l4, 0444, driver_show_l4, NULL);

... would you notice it at a glance that it contains two security holes?

While the weird permissions in this:

+		__ATTR(l1, PERM_r__r__r__,  driver_show_l4, NULL);
+		__ATTR(l3, PERM_r__r__rw_,  driver_show_l4, NULL);
+		__ATTR(l2, PERM_sr__r__r__, driver_show_l4, NULL);
+		__ATTR(l4, PERM_r__r__r__,  driver_show_l4, NULL);

Wouln't even build, because the dangerous patterns of PERM_r__r__rw_ or 
PERM_sr__r__r__ are not defined to begin with.

The patch below adds them to stat.h.

Thanks,

	Ingo

 include/linux/stat.h | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

Comments

Greg KH Aug. 3, 2016, 8:28 a.m. UTC | #1
On Wed, Aug 03, 2016 at 10:11:40AM +0200, Ingo Molnar wrote:
> An added advantage would be that during review it would stick out like a sore 
> thumb if anyone used a 'weird' permission variant.
> 
> For example, if you saw these lines in a driver patch:
> 
> +	__ATTR(l1, 0444, driver_show_l4, NULL);
> +		__ATTR(l3, 0446, driver_show_l4, NULL);
> +			__ATTR(l2, 04444, driver_show_l4, NULL);
> +		__ATTR(l4, 0444, driver_show_l4, NULL);
> 
> ... would you notice it at a glance that it contains two security holes?

I've tried to deal with that in the past with the __ATTR_RW() and
__ATTR_RO() and __ATTR_WO() macros that more should be using.  I swept
the tree a few years ago to try to fix up most of them, but I know I
didn't catch them all, and more files have been added since then.

> While the weird permissions in this:
> 
> +		__ATTR(l1, PERM_r__r__r__,  driver_show_l4, NULL);
> +		__ATTR(l3, PERM_r__r__rw_,  driver_show_l4, NULL);
> +		__ATTR(l2, PERM_sr__r__r__, driver_show_l4, NULL);
> +		__ATTR(l4, PERM_r__r__r__,  driver_show_l4, NULL);
> 
> Wouln't even build, because the dangerous patterns of PERM_r__r__rw_ or 
> PERM_sr__r__r__ are not defined to begin with.

Because of that, odds are people will just stick to the octal numbers,
because they think they want something other than the ones you defined
for foolish reasons :)

That being said, I do like them much better than the macros we have
today, which I always have to go and look up every time I see them...

thanks,

greg k-h
Ingo Molnar Aug. 3, 2016, 8:39 a.m. UTC | #2
* Greg Kroah-Hartman <gregkh@linuxfoundation.org> wrote:

> On Wed, Aug 03, 2016 at 10:11:40AM +0200, Ingo Molnar wrote:
> > An added advantage would be that during review it would stick out like a sore 
> > thumb if anyone used a 'weird' permission variant.
> > 
> > For example, if you saw these lines in a driver patch:
> > 
> > +	__ATTR(l1, 0444, driver_show_l4, NULL);
> > +		__ATTR(l3, 0446, driver_show_l4, NULL);
> > +			__ATTR(l2, 04444, driver_show_l4, NULL);
> > +		__ATTR(l4, 0444, driver_show_l4, NULL);
> > 
> > ... would you notice it at a glance that it contains two security holes?
> 
> I've tried to deal with that in the past with the __ATTR_RW() and
> __ATTR_RO() and __ATTR_WO() macros that more should be using.  I swept
> the tree a few years ago to try to fix up most of them, but I know I
> didn't catch them all, and more files have been added since then.
> 
> > While the weird permissions in this:
> > 
> > +		__ATTR(l1, PERM_r__r__r__,  driver_show_l4, NULL);
> > +		__ATTR(l3, PERM_r__r__rw_,  driver_show_l4, NULL);
> > +		__ATTR(l2, PERM_sr__r__r__, driver_show_l4, NULL);
> > +		__ATTR(l4, PERM_r__r__r__,  driver_show_l4, NULL);
> > 
> > Wouln't even build, because the dangerous patterns of PERM_r__r__rw_ or 
> > PERM_sr__r__r__ are not defined to begin with.
> 
> Because of that, odds are people will just stick to the octal numbers,
> because they think they want something other than the ones you defined
> for foolish reasons :)

For code I maintain I'd insist on contributors using the human readable versions, 
because in the past I've mixed up octals (and the symbolic helpers we have today) 
myself and I find the 'ls -l' format much easier to read because that's the 
primary file permission format I see every day working on code.

> That being said, I do like them much better than the macros we have today, which 
> I always have to go and look up every time I see them...

Same here!

I'm sure core VFS developers know all of the octals and the helpers by heart, but 
the set of maintainers accepting debugfs and sysfs file permission patches is much 
wider than that, so every little bit of clarity helps.

Thanks,

	Ingo
Willy Tarreau Aug. 3, 2016, 9:21 a.m. UTC | #3
On Wed, Aug 03, 2016 at 10:39:03AM +0200, Ingo Molnar wrote:
> > > While the weird permissions in this:
> > > 
> > > +		__ATTR(l1, PERM_r__r__r__,  driver_show_l4, NULL);
> > > +		__ATTR(l3, PERM_r__r__rw_,  driver_show_l4, NULL);
> > > +		__ATTR(l2, PERM_sr__r__r__, driver_show_l4, NULL);
> > > +		__ATTR(l4, PERM_r__r__r__,  driver_show_l4, NULL);
> > > 
> > > Wouln't even build, because the dangerous patterns of PERM_r__r__rw_ or 
> > > PERM_sr__r__r__ are not defined to begin with.
> > 
> > Because of that, odds are people will just stick to the octal numbers,
> > because they think they want something other than the ones you defined
> > for foolish reasons :)
> 
> For code I maintain I'd insist on contributors using the human readable versions, 
> because in the past I've mixed up octals (and the symbolic helpers we have today) 
> myself and I find the 'ls -l' format much easier to read because that's the 
> primary file permission format I see every day working on code.

FWIW, the only "human readable" ones for me are the octal ones, which are
also the same as those I'm using every day with "chmod" or "find" and that
I find hard to get wrong. But I agree that the PERM_* idea above are a nice
alternative since they match the "ls -l" output, and you can even add the
directory flag there with "d" like "ls" does. You could also have PERM_0444
and similar for those who are more at ease with the octal numers without
defining the few ones that are definitely wrong, as a safety belt.

> > That being said, I do like them much better than the macros we have today, which 
> > I always have to go and look up every time I see them...
> 
> Same here!

Same for me. I never use S_I* and never know where to look for their
definitions when I see them.

Willy
Marcel Holtmann Aug. 3, 2016, 9:53 a.m. UTC | #4
Hi Greg,

>> An added advantage would be that during review it would stick out like a sore 
>> thumb if anyone used a 'weird' permission variant.
>> 
>> For example, if you saw these lines in a driver patch:
>> 
>> +	__ATTR(l1, 0444, driver_show_l4, NULL);
>> +		__ATTR(l3, 0446, driver_show_l4, NULL);
>> +			__ATTR(l2, 04444, driver_show_l4, NULL);
>> +		__ATTR(l4, 0444, driver_show_l4, NULL);
>> 
>> ... would you notice it at a glance that it contains two security holes?
> 
> I've tried to deal with that in the past with the __ATTR_RW() and
> __ATTR_RO() and __ATTR_WO() macros that more should be using.  I swept
> the tree a few years ago to try to fix up most of them, but I know I
> didn't catch them all, and more files have been added since then.

I said in another response that maybe module_param_rw and module_param_ro will make some sense. Not sure if they are easier to read or not. I mean for each usage, we could look at the tree and see what values are actually used. My bet is that for module_param only a few ones are used. I have the feeling it is 0444 or 0644 and nothing else. Maybe some outlaws with 0400 and 0600 that don't even need to be that secretive.

Regards

Marcel
Joe Perches Aug. 3, 2016, 3:49 p.m. UTC | #5
On Wed, 2016-08-03 at 10:11 +0200, Ingo Molnar wrote:
> * Linus Torvalds <torvalds@linux-foundation.org> wrote:
> > [ So I answered similarly to another patch, but I'll just re-iterate
> > and change the subject line so that it stands out a bit from the
> > millions of actual patches ]
> > 
> > On Tue, Aug 2, 2016 at 1:42 PM, Pavel Machek <pavel@ucw.cz> wrote:
> > > Everyone knows what 0644 is, but noone can read S_IRUSR | S_IWUSR |
> > > S_IRCRP | S_IROTH (*). Please don't do this.
> > Absolutely. It's *much* easier to parse and understand the octal
> > numbers, while the symbolic macro names are just random line noise and
> > hard as hell to understand. You really have to think about it.
> > 
> > So we should rather go the other way: convert existing bad symbolic
> > permission bit macro use to just use the octal numbers.
> In addition to that I'd love to have something even easier to read, a few common 
> variants of the permissions field of 'ls -l' pre-defined. I did some quick 
> grepping, and collected the main variants that are in use:
> 
> 		PERM_r________	0400
> 		PERM_r__r_____	0440
> 		PERM_r__r__r__	0444

[etc]

While the proposed PERM_ variants are easily read,
using a single style instead of 2+ incompatible
symbolic styles makes treewide misuse identification
via grep style tools easier.
Pavel Machek Aug. 3, 2016, 4:38 p.m. UTC | #6
On Wed 2016-08-03 10:11:40, Ingo Molnar wrote:
> 
> * Linus Torvalds <torvalds@linux-foundation.org> wrote:
> 
> > [ So I answered similarly to another patch, but I'll just re-iterate
> > and change the subject line so that it stands out a bit from the
> > millions of actual patches ]
> > 
> > On Tue, Aug 2, 2016 at 1:42 PM, Pavel Machek <pavel@ucw.cz> wrote:
> > >
> > > Everyone knows what 0644 is, but noone can read S_IRUSR | S_IWUSR |
> > > S_IRCRP | S_IROTH (*). Please don't do this.
> > 
> > Absolutely. It's *much* easier to parse and understand the octal
> > numbers, while the symbolic macro names are just random line noise and
> > hard as hell to understand. You really have to think about it.
> > 
> > So we should rather go the other way: convert existing bad symbolic
> > permission bit macro use to just use the octal numbers.
> 
> In addition to that I'd love to have something even easier to read, a few common 
> variants of the permissions field of 'ls -l' pre-defined. I did some quick 
> grepping, and collected the main variants that are in use:
> 
> 		PERM_r________	0400
> 		PERM_r__r_____	0440
> 		PERM_r__r__r__	0444

I see 0400 and 0444 making sense, but does 0440 really make sense?
I assume it will be uid/gid 0/0? Is gid 0 really estabilished well
enough to give it special permissions?

And yes, these macros actually help readability.

> 		PERM__wx______	0300
> 		PERM__wx_wx___	0330
> 		PERM__wx_wx_wx	0333

Uh. This is for sysfs. Do we event want any __x variants? _wx
would certainly be strange.

(And yes, we can keep people from using strange permissions by simply
not defining those macros.)

> Allowing these would be nice too, because there were cases in the past where 
> people messed up the octal representation or our internal symbolic helpers,
> but this representation is fundamentally self-describing and pretty 'fool proof'.
> 
> An added advantage would be that during review it would stick out like a sore 
> thumb if anyone used a 'weird' permission variant.
> 
> For example, if you saw these lines in a driver patch:
> 
> +	__ATTR(l1, 0444, driver_show_l4, NULL);
> +		__ATTR(l3, 0446, driver_show_l4, NULL);
> +			__ATTR(l2, 04444, driver_show_l4, NULL);
> +		__ATTR(l4, 0444, driver_show_l4, NULL);
> 
> ... would you notice it at a glance that it contains two security holes?

I see two bugs but only one hole. How can you exploit s-bit without corresponding x-bit?

I'd delete these: I don't think we should encourage their use:

> +#define PERM_r__r_____	0440
> +#define PERM_rw_r_____	0640
> +#define PERM_rw_rw_r__	0664
> +
> +#define PERM__w__w__w_	0222
> +
> +#define PERM_r_x______	0500
> +#define PERM_r_xr_x___	0550
> +#define PERM_r_xr_xr_x	0555
> +
> +#define PERM_rwx______	0700
> +#define PERM_rwxr_x___	0750
> +#define PERM_rwxr_xr_x	0755
> +#define PERM_rwxrwxr_x	0775
> +#define PERM_rwxrwxrwx	0777
> +
> +#define PERM__wx______	0300
> +#define PERM__wx_wx___	0330
> +#define PERM__wx_wx_wx	0333

									Pavel
diff mbox

Patch

diff --git a/include/linux/stat.h b/include/linux/stat.h
index 075cb0c7eb2a..863d5563427f 100644
--- a/include/linux/stat.h
+++ b/include/linux/stat.h
@@ -5,6 +5,38 @@ 
 #include <asm/stat.h>
 #include <uapi/linux/stat.h>
 
+/*
+ * Human readable symbolic definitions for common
+ * file permissions:
+ */
+#define PERM_r________	0400
+#define PERM_r__r_____	0440
+#define PERM_r__r__r__	0444
+
+#define PERM_rw_______	0600
+#define PERM_rw_r_____	0640
+#define PERM_rw_r__r__	0644
+#define PERM_rw_rw_r__	0664
+#define PERM_rw_rw_rw_	0666
+
+#define PERM__w_______	0200
+#define PERM__w__w____	0220
+#define PERM__w__w__w_	0222
+
+#define PERM_r_x______	0500
+#define PERM_r_xr_x___	0550
+#define PERM_r_xr_xr_x	0555
+
+#define PERM_rwx______	0700
+#define PERM_rwxr_x___	0750
+#define PERM_rwxr_xr_x	0755
+#define PERM_rwxrwxr_x	0775
+#define PERM_rwxrwxrwx	0777
+
+#define PERM__wx______	0300
+#define PERM__wx_wx___	0330
+#define PERM__wx_wx_wx	0333
+
 #define S_IRWXUGO	(S_IRWXU|S_IRWXG|S_IRWXO)
 #define S_IALLUGO	(S_ISUID|S_ISGID|S_ISVTX|S_IRWXUGO)
 #define S_IRUGO		(S_IRUSR|S_IRGRP|S_IROTH)