public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] userns: improve uid/gid map collision detection
@ 2013-01-23 16:02 Aristeu Rozanski
  2013-01-24  4:44 ` Serge E. Hallyn
  0 siblings, 1 reply; 8+ messages in thread
From: Aristeu Rozanski @ 2013-01-23 16:02 UTC (permalink / raw)
  To: linux-kernel; +Cc: Eric W. Biederman, Serge E. Hallyn

Initial implementation of the uid/gid maps will enforce that the
maps should be in order and would prevent a use case like this from
being used:
	0	1000	1
	48	500	1

since the second entry both values should be bigger than the previous.
This patch implements a more elaborate collision detection allowing any
order to be used.

Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: Aristeu Rozanski <aris@redhat.com>

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index 2b042c4..fb0e492 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -521,6 +521,28 @@ struct seq_operations proc_projid_seq_operations = {
 
 static DEFINE_MUTEX(id_map_mutex);
 
+#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
+static inline int extent_collision(struct uid_gid_map *new_map,
+				   struct uid_gid_extent *extent)
+{
+	int i;
+	struct uid_gid_extent *cur;
+
+	for (i = 0; i < new_map->nr_extents; i++) {
+		cur = &new_map->extent[i];
+		if (in_range(extent->first, cur->first, cur->count) ||
+		    in_range(extent->first + extent->count, cur->first,
+			     cur->count))
+			return 1;
+		if (in_range(extent->lower_first, cur->lower_first,
+			     cur->count) ||
+		    in_range(extent->lower_first + extent->count,
+			     cur->lower_first, cur->count))
+			return 1;
+	}
+	return 0;
+}
+
 static ssize_t map_write(struct file *file, const char __user *buf,
 			 size_t count, loff_t *ppos,
 			 int cap_setid,
@@ -634,10 +656,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 		if ((extent->lower_first + extent->count) <= extent->lower_first)
 			goto out;
 
-		/* For now only accept extents that are strictly in order */
-		if (last &&
-		    (((last->first + last->count) > extent->first) ||
-		     ((last->lower_first + last->count) > extent->lower_first)))
+		if (extent_collision(&new_map, extent))
 			goto out;
 
 		new_map.nr_extents++;


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] userns: improve uid/gid map collision detection
  2013-01-23 16:02 [PATCH] userns: improve uid/gid map collision detection Aristeu Rozanski
@ 2013-01-24  4:44 ` Serge E. Hallyn
  2013-01-24 15:28   ` [PATCH v2] " Aristeu Rozanski
  0 siblings, 1 reply; 8+ messages in thread
From: Serge E. Hallyn @ 2013-01-24  4:44 UTC (permalink / raw)
  To: Aristeu Rozanski; +Cc: linux-kernel, Eric W. Biederman, Serge E. Hallyn

Quoting Aristeu Rozanski (aris@redhat.com):
> Initial implementation of the uid/gid maps will enforce that the
> maps should be in order and would prevent a use case like this from
> being used:
> 	0	1000	1
> 	48	500	1
> 
> since the second entry both values should be bigger than the previous.
> This patch implements a more elaborate collision detection allowing any
> order to be used.
> 
> Cc: "Eric W. Biederman" <ebiederm@xmission.com>
> Cc: "Serge E. Hallyn" <serge@hallyn.com>

I *think* that looks just right :)

Acked-by: Serge Hallyn <serge.hallyn@canonical.com>

> Signed-off-by: Aristeu Rozanski <aris@redhat.com>
> 
> diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
> index 2b042c4..fb0e492 100644
> --- a/kernel/user_namespace.c
> +++ b/kernel/user_namespace.c
> @@ -521,6 +521,28 @@ struct seq_operations proc_projid_seq_operations = {
>  
>  static DEFINE_MUTEX(id_map_mutex);
>  
> +#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
> +static inline int extent_collision(struct uid_gid_map *new_map,
> +				   struct uid_gid_extent *extent)
> +{
> +	int i;
> +	struct uid_gid_extent *cur;
> +
> +	for (i = 0; i < new_map->nr_extents; i++) {
> +		cur = &new_map->extent[i];
> +		if (in_range(extent->first, cur->first, cur->count) ||
> +		    in_range(extent->first + extent->count, cur->first,
> +			     cur->count))
> +			return 1;
> +		if (in_range(extent->lower_first, cur->lower_first,
> +			     cur->count) ||
> +		    in_range(extent->lower_first + extent->count,
> +			     cur->lower_first, cur->count))
> +			return 1;
> +	}
> +	return 0;
> +}
> +
>  static ssize_t map_write(struct file *file, const char __user *buf,
>  			 size_t count, loff_t *ppos,
>  			 int cap_setid,
> @@ -634,10 +656,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
>  		if ((extent->lower_first + extent->count) <= extent->lower_first)
>  			goto out;
>  
> -		/* For now only accept extents that are strictly in order */
> -		if (last &&
> -		    (((last->first + last->count) > extent->first) ||
> -		     ((last->lower_first + last->count) > extent->lower_first)))
> +		if (extent_collision(&new_map, extent))
>  			goto out;
>  
>  		new_map.nr_extents++;

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-24  4:44 ` Serge E. Hallyn
@ 2013-01-24 15:28   ` Aristeu Rozanski
  2013-01-25  0:46     ` Andrew Morton
  0 siblings, 1 reply; 8+ messages in thread
From: Aristeu Rozanski @ 2013-01-24 15:28 UTC (permalink / raw)
  To: linux-kernel; +Cc: Serge E. Hallyn, Eric W. Biederman, Andrew Morton

userns: improve uid/gid map collision detection

Initial implementation of the uid/gid maps (/proc/<pid>/{u,g}id_map) will
enforce that the UID and GID maps be written in strict order as a simple
way to check for range collision:
	local id	mapped to	count/range
	0		1000		50
	(ids 0-50 get mapped to 1000-1050)
	100		2120		10
	500		5000		200
so for each new entry, local id must be bigger than last local id (plus
count) and the ids it maps to also needs to be bigger than the last
entry (plus count).

This makes impossible to have a use case like this:
	local id	mapped to	count/range
	0		1000		1
	48		500		20

because while 48+20 > 0+1, 500+20 < 1000+1.

This patch implements a more elaborate collision detection allowing any
order to be used.

v2: improved the patch description as requested by Andrew

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: "Serge E. Hallyn" <serge@hallyn.com>
Cc: linux-security-module@vger.kernel.org
Signed-off-by: Aristeu Rozanski <aris@redhat.com>

diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index 2b042c4..fb0e492 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -521,6 +521,28 @@ struct seq_operations proc_projid_seq_operations = {
 
 static DEFINE_MUTEX(id_map_mutex);
 
+#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
+static inline int extent_collision(struct uid_gid_map *new_map,
+				   struct uid_gid_extent *extent)
+{
+	int i;
+	struct uid_gid_extent *cur;
+
+	for (i = 0; i < new_map->nr_extents; i++) {
+		cur = &new_map->extent[i];
+		if (in_range(extent->first, cur->first, cur->count) ||
+		    in_range(extent->first + extent->count, cur->first,
+			     cur->count))
+			return 1;
+		if (in_range(extent->lower_first, cur->lower_first,
+			     cur->count) ||
+		    in_range(extent->lower_first + extent->count,
+			     cur->lower_first, cur->count))
+			return 1;
+	}
+	return 0;
+}
+
 static ssize_t map_write(struct file *file, const char __user *buf,
 			 size_t count, loff_t *ppos,
 			 int cap_setid,
@@ -634,10 +656,7 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 		if ((extent->lower_first + extent->count) <= extent->lower_first)
 			goto out;
 
-		/* For now only accept extents that are strictly in order */
-		if (last &&
-		    (((last->first + last->count) > extent->first) ||
-		     ((last->lower_first + last->count) > extent->lower_first)))
+		if (extent_collision(&new_map, extent))
 			goto out;
 
 		new_map.nr_extents++;

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-24 15:28   ` [PATCH v2] " Aristeu Rozanski
@ 2013-01-25  0:46     ` Andrew Morton
  2013-01-25  2:00       ` Eric W. Biederman
  2013-01-25 14:03       ` Aristeu Rozanski
  0 siblings, 2 replies; 8+ messages in thread
From: Andrew Morton @ 2013-01-25  0:46 UTC (permalink / raw)
  To: Aristeu Rozanski; +Cc: linux-kernel, Serge E. Hallyn, Eric W. Biederman

On Thu, 24 Jan 2013 10:28:59 -0500
Aristeu Rozanski <aris@redhat.com> wrote:

> userns: improve uid/gid map collision detection
> 
> Initial implementation of the uid/gid maps (/proc/<pid>/{u,g}id_map) will
> enforce that the UID and GID maps be written in strict order as a simple
> way to check for range collision:
> 	local id	mapped to	count/range
> 	0		1000		50
> 	(ids 0-50 get mapped to 1000-1050)
> 	100		2120		10
> 	500		5000		200
> so for each new entry, local id must be bigger than last local id (plus
> count) and the ids it maps to also needs to be bigger than the last
> entry (plus count).
> 
> This makes impossible to have a use case like this:
> 	local id	mapped to	count/range
> 	0		1000		1
> 	48		500		20
> 
> because while 48+20 > 0+1, 500+20 < 1000+1.
> 
> This patch implements a more elaborate collision detection allowing any
> order to be used.
> 
> v2: improved the patch description as requested by Andrew

Thanks.

> --- a/kernel/user_namespace.c
> +++ b/kernel/user_namespace.c
> @@ -521,6 +521,28 @@ struct seq_operations proc_projid_seq_operations = {
>  
>  static DEFINE_MUTEX(id_map_mutex);
>  
> +#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))

eek, a macro!  Macros are always bad.

This one is bad because

a) it's a macro

b) it evaluates its args multiple times and hence will cause nasty
   bugs if called with expressions-with-side-effects.

c) it evaluates its args multiple times and if called with
   non-trivial expressions the compiler might not be able to CSE those
   expressions, leading to code bloat.

Add lo, this patch:

--- a/kernel/user_namespace.c~userns-improve-uid-gid-map-collision-detection-fix
+++ a/kernel/user_namespace.c
@@ -521,7 +521,11 @@ struct seq_operations proc_projid_seq_op
 
 static DEFINE_MUTEX(id_map_mutex);
 
-#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
+static bool in_range(u32 b, u32 first, u32 len)
+{
+	return b >= first && b < first + len;
+}
+
 static inline int extent_collision(struct uid_gid_map *new_map,
 				   struct uid_gid_extent *extent)
 {

reduces the user_namespace.o text from 4822 bytes to 4727 with
gcc-4.4.4.  This is a remarkably large difference.


btw, what the heck is up with CONFIG_UIDGID_CONVERTED?  That thing
prevents userns from being compiled in an allmodconfig build, which is
rather undesirable.  Isn't there a better, more user-friendly way of
doing this?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-25  0:46     ` Andrew Morton
@ 2013-01-25  2:00       ` Eric W. Biederman
  2013-01-25 14:03       ` Aristeu Rozanski
  1 sibling, 0 replies; 8+ messages in thread
From: Eric W. Biederman @ 2013-01-25  2:00 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Aristeu Rozanski, linux-kernel, Serge E. Hallyn


I will look at this patch in a bit.  I already have a similar patch
in my queue, although I don't know if I have pushed that out yet.


Andrew Morton <akpm@linux-foundation.org> writes:

> btw, what the heck is up with CONFIG_UIDGID_CONVERTED?  That thing
> prevents userns from being compiled in an allmodconfig build, which is
> rather undesirable.  Isn't there a better, more user-friendly way of
> doing this?

CONFIG_UIDGID_CONVERTED is what keeps the allmodconfig building with user
namespaces enabled.

The practical issue is that the uid_t, gid_t converstion to kuid_t and
kgid_t is not quite done.  And it takes a lot of pain staking review in
some cases to get it done.

In places where the conversion is not complete the code fails to compile
preventing the introduction of security holes.

If you look in init/Kconfig at the definition of CONFIG_UIDGID_CONVERTED
you see the list of filesystems that still need changes.

I have the basic version of those changes queued up, and I believe those
changes are correct.  However I need to break them into smaller patches
to make the changes obviously correct.  One of the things I have
discovered along the way of making these conversions is that I need to
push kuid_t and kgid_t as deep and as thoroughly into the code as I can
or I will fail to place conversions where they need to be.

The amount of work required on those final pieces appears to be
proportional to the size of the filesystem rather than proportional to
the size of the changes to those filesystems.  Because there is so much
code that must be reviewed and understood.

I am hoping I can get the conversions done and CONFIG_UIDGID_CONVERTED
can be killed for 3.9 but we will see.

Eric



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-25  0:46     ` Andrew Morton
  2013-01-25  2:00       ` Eric W. Biederman
@ 2013-01-25 14:03       ` Aristeu Rozanski
  2013-01-26  2:31         ` Eric W. Biederman
  1 sibling, 1 reply; 8+ messages in thread
From: Aristeu Rozanski @ 2013-01-25 14:03 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, Serge E. Hallyn, Eric W. Biederman

On Thu, Jan 24, 2013 at 04:46:12PM -0800, Andrew Morton wrote:
> eek, a macro!  Macros are always bad.
> 
> This one is bad because
> 
> a) it's a macro
> 
> b) it evaluates its args multiple times and hence will cause nasty
>    bugs if called with expressions-with-side-effects.
> 
> c) it evaluates its args multiple times and if called with
>    non-trivial expressions the compiler might not be able to CSE those
>    expressions, leading to code bloat.
> 
> Add lo, this patch:
> 
> --- a/kernel/user_namespace.c~userns-improve-uid-gid-map-collision-detection-fix
> +++ a/kernel/user_namespace.c
> @@ -521,7 +521,11 @@ struct seq_operations proc_projid_seq_op
>  
>  static DEFINE_MUTEX(id_map_mutex);
>  
> -#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
> +static bool in_range(u32 b, u32 first, u32 len)
> +{
> +	return b >= first && b < first + len;
> +}
> +
>  static inline int extent_collision(struct uid_gid_map *new_map,
>  				   struct uid_gid_extent *extent)
>  {
> 
> reduces the user_namespace.o text from 4822 bytes to 4727 with
> gcc-4.4.4.  This is a remarkably large difference.

thanks Andrew

(I see Eric already answered about the config option)

-- 
Aristeu


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-25 14:03       ` Aristeu Rozanski
@ 2013-01-26  2:31         ` Eric W. Biederman
  2013-01-28 14:25           ` Aristeu Rozanski
  0 siblings, 1 reply; 8+ messages in thread
From: Eric W. Biederman @ 2013-01-26  2:31 UTC (permalink / raw)
  To: Aristeu Rozanski; +Cc: Andrew Morton, linux-kernel, Serge E. Hallyn

Aristeu Rozanski <aris@redhat.com> writes:

> On Thu, Jan 24, 2013 at 04:46:12PM -0800, Andrew Morton wrote:
>> eek, a macro!  Macros are always bad.
>> 
>> This one is bad because
>> 
>> a) it's a macro
>> 
>> b) it evaluates its args multiple times and hence will cause nasty
>>    bugs if called with expressions-with-side-effects.
>> 
>> c) it evaluates its args multiple times and if called with
>>    non-trivial expressions the compiler might not be able to CSE those
>>    expressions, leading to code bloat.
>> 
>> Add lo, this patch:
>> 
>> --- a/kernel/user_namespace.c~userns-improve-uid-gid-map-collision-detection-fix
>> +++ a/kernel/user_namespace.c
>> @@ -521,7 +521,11 @@ struct seq_operations proc_projid_seq_op
>>  
>>  static DEFINE_MUTEX(id_map_mutex);
>>  
>> -#define in_range(b,first,len) ((b)>=(first)&&(b)<(first)+(len))
>> +static bool in_range(u32 b, u32 first, u32 len)
>> +{
>> +	return b >= first && b < first + len;
>> +}
>> +
>>  static inline int extent_collision(struct uid_gid_map *new_map,
>>  				   struct uid_gid_extent *extent)
>>  {
>> 
>> reduces the user_namespace.o text from 4822 bytes to 4727 with
>> gcc-4.4.4.  This is a remarkably large difference.
>
> thanks Andrew
>
> (I see Eric already answered about the config option)

Aritsteu after looking at both my version and yours I am going with
mine.  While my code is a little wordier I have half the number of
comparisons your code does, and I took the time to kill the variable
introducing a function to test for range collisions makes unnecessary.
On Andrews size metric my version seems noticably smaller as well.

 size $PWD-build/kernel/user_namespace.o
   text	   data	    bss	    dec	    hex	filename
   4376	    144	      0	   4520	   11a8	/home/eric/projects/linux/linux-userns-devel-build/kernel/user_namespace.o


Short of something unexpected I plan to push all my code to linux-next
sometime tomorrow.

Eric


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] userns: improve uid/gid map collision detection
  2013-01-26  2:31         ` Eric W. Biederman
@ 2013-01-28 14:25           ` Aristeu Rozanski
  0 siblings, 0 replies; 8+ messages in thread
From: Aristeu Rozanski @ 2013-01-28 14:25 UTC (permalink / raw)
  To: Eric W. Biederman; +Cc: Andrew Morton, linux-kernel, Serge E. Hallyn

On Fri, Jan 25, 2013 at 06:31:37PM -0800, Eric W. Biederman wrote:
> Aritsteu after looking at both my version and yours I am going with

one extra t.

> mine.  While my code is a little wordier I have half the number of
> comparisons your code does, and I took the time to kill the variable
> introducing a function to test for range collisions makes unnecessary.
> On Andrews size metric my version seems noticably smaller as well.
> 
>  size $PWD-build/kernel/user_namespace.o
>    text	   data	    bss	    dec	    hex	filename
>    4376	    144	      0	   4520	   11a8	/home/eric/projects/linux/linux-userns-devel-build/kernel/user_namespace.o
> 
> 
> Short of something unexpected I plan to push all my code to linux-next
> sometime tomorrow.

sure, no problem
Thanks Eric

-- 
Aristeu


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-01-28 14:25 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-23 16:02 [PATCH] userns: improve uid/gid map collision detection Aristeu Rozanski
2013-01-24  4:44 ` Serge E. Hallyn
2013-01-24 15:28   ` [PATCH v2] " Aristeu Rozanski
2013-01-25  0:46     ` Andrew Morton
2013-01-25  2:00       ` Eric W. Biederman
2013-01-25 14:03       ` Aristeu Rozanski
2013-01-26  2:31         ` Eric W. Biederman
2013-01-28 14:25           ` Aristeu Rozanski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox