Skip to contentSkip to conversation

FDPIC: Ignore the loader's PT_GNU_STACK when calculating the stack size

PATCH In review David Howells 4 participants 16 messages Jul 1, 2009 – Jul 18, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 FDPIC: Ignore the loader's PT_GNU_STACK when calculating the stack size

Ignore the loader's PT_GNU_STACK when calculating the stack size, and only consider the executable's PT_GNU_STACK, assuming the executable has one.

Currently the behaviour is to take the largest stack size and use that, but that means you can't reduce the stack size in the executable. The loader's stack size should probably only be used when executing the loader directly.

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

---

 fs/binfmt_elf_fdpic.c |   19 +++++++++++--------
 1 files changed, 11 insertions(+), 8 deletions(-)
fs/binfmt_elf_fdpic.c +11 -8
index 81ca047..58c49fa 100644
@@ -283,20 +283,23 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,
 	}
 
 	stack_size = exec_params.stack_size;
-	if (stack_size < interp_params.stack_size)
-		stack_size = interp_params.stack_size;
-
 	if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
 		executable_stack = EXSTACK_ENABLE_X;
 	else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
 		executable_stack = EXSTACK_DISABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
-		executable_stack = EXSTACK_ENABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
-		executable_stack = EXSTACK_DISABLE_X;
-	else
+	else 
 		executable_stack = EXSTACK_DEFAULT;
 
+	if (stack_size == 0) {
+		stack_size = interp_params.stack_size;
+		if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
+			executable_stack = EXSTACK_ENABLE_X;
+		else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
+			executable_stack = EXSTACK_DISABLE_X;
+		else
+			executable_stack = EXSTACK_DEFAULT;
+	}
+
 	retval = -ENOEXEC;
 	if (stack_size == 0)
 		goto error;
#1 lore raw
Quoted 3 lines · David Howells

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

every toolchain ive seen sets the stack size in the linker to the default value, and the only way to change that is to manually set the size yourself with the relevant command line option. so it's only going to break people who have been doing it wrong themselves -- setting the stack size to a smaller option than is acceptable.

Quoted 2 lines

-       else
+       else

this change just adds trailing whitespace ...

otherwise, just tested it and it works for me, thanks

-mike

#3 lore raw

PATCH 1/1 FDPIC: Ignore the loader's PT_GNU_STACK when calculating the stack size [ver #2]

In reply to David Howells

Ignore the loader's PT_GNU_STACK when calculating the stack size, and only consider the executable's PT_GNU_STACK, assuming the executable has one.

Currently the behaviour is to take the largest stack size and use that, but that means you can't reduce the stack size in the executable. The loader's stack size should probably only be used when executing the loader directly.

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

---

 fs/binfmt_elf_fdpic.c |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)
fs/binfmt_elf_fdpic.c +10 -7
index 81ca047..a1e6365 100644
@@ -283,20 +283,23 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,
 	}
 
 	stack_size = exec_params.stack_size;
-	if (stack_size < interp_params.stack_size)
-		stack_size = interp_params.stack_size;
-
 	if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
 		executable_stack = EXSTACK_ENABLE_X;
 	else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
 		executable_stack = EXSTACK_DISABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
-		executable_stack = EXSTACK_ENABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
-		executable_stack = EXSTACK_DISABLE_X;
 	else
 		executable_stack = EXSTACK_DEFAULT;
 
+	if (stack_size == 0) {
+		stack_size = interp_params.stack_size;
+		if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
+			executable_stack = EXSTACK_ENABLE_X;
+		else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
+			executable_stack = EXSTACK_DISABLE_X;
+		else
+			executable_stack = EXSTACK_DEFAULT;
+	}
+
 	retval = -ENOEXEC;
 	if (stack_size == 0)
 		goto error;
#4 lore raw

PATCH 1/1 FDPIC: Ignore the loader's PT_GNU_STACK when calculating the stack size [ver #2]

In reply to David Howells

Ignore the loader's PT_GNU_STACK when calculating the stack size, and only consider the executable's PT_GNU_STACK, assuming the executable has one.

Currently the behaviour is to take the largest stack size and use that, but that means you can't reduce the stack size in the executable. The loader's stack size should probably only be used when executing the loader directly.

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

---

 fs/binfmt_elf_fdpic.c |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)
fs/binfmt_elf_fdpic.c +10 -7
index 81ca047..a1e6365 100644
@@ -283,20 +283,23 @@ static int load_elf_fdpic_binary(struct linux_binprm *bprm,
 	}
 
 	stack_size = exec_params.stack_size;
-	if (stack_size < interp_params.stack_size)
-		stack_size = interp_params.stack_size;
-
 	if (exec_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
 		executable_stack = EXSTACK_ENABLE_X;
 	else if (exec_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
 		executable_stack = EXSTACK_DISABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
-		executable_stack = EXSTACK_ENABLE_X;
-	else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
-		executable_stack = EXSTACK_DISABLE_X;
 	else
 		executable_stack = EXSTACK_DEFAULT;
 
+	if (stack_size == 0) {
+		stack_size = interp_params.stack_size;
+		if (interp_params.flags & ELF_FDPIC_FLAG_EXEC_STACK)
+			executable_stack = EXSTACK_ENABLE_X;
+		else if (interp_params.flags & ELF_FDPIC_FLAG_NOEXEC_STACK)
+			executable_stack = EXSTACK_DISABLE_X;
+		else
+			executable_stack = EXSTACK_DEFAULT;
+	}
+
 	retval = -ENOEXEC;
 	if (stack_size == 0)
 		goto error;
Acked-by on v1 1/1
#5 lore raw
In reply to David Howells
Quoted 7 lines · David Howells

Ignore the loader's PT_GNU_STACK when calculating the stack size, and only consider the executable's PT_GNU_STACK, assuming the executable has one.

Currently the behaviour is to take the largest stack size and use that, but that means you can't reduce the stack size in the executable. The loader's stack size should probably only be used when executing the loader directly.

Quoted 5 lines

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

Works for me too.

#6 lore raw
In reply to David Howells
Quoted 10 lines · David Howells

Ignore the loader's PT_GNU_STACK when calculating the stack size, and only consider the executable's PT_GNU_STACK, assuming the executable has one.

Currently the behaviour is to take the largest stack size and use that, but that means you can't reduce the stack size in the executable. The loader's stack size should probably only be used when executing the loader directly.

WARNING: This patch is slightly dangerous - it may render a system inoperable if the loader's stack size is larger than that of important executables, and the system relies unknowingly on this increasing the size of the stack.

The patch seems wrong to me; loader needs the stack, too, right?

What about making sure that the loader specifies reasonable stack size, instead?

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#7 lore raw
Quoted 4 lines · Pavel Machek

The patch seems wrong to me; loader needs the stack, too, right?

What about making sure that the loader specifies reasonable stack size, instead?

The loader doesn't need its own stack, unless it _is_ the executable. It uses the executable's stack. The problem is that the executable and the loader can both specify the stack size for NOMMU, but which one do we pick, or do we consider both.

David

#8 lore raw
Quoted 11 lines · David Howells
Quoted 4 lines · Pavel Machek

The patch seems wrong to me; loader needs the stack, too, right?

What about making sure that the loader specifies reasonable stack size, instead?

The loader doesn't need its own stack, unless it _is_ the executable. It uses the executable's stack. The problem is that the executable and the loader can both specify the stack size for NOMMU, but which one do we pick, or do we consider both.

Imageine loader needs 64K, while executable only needs 4K. You surely want to execute it with 64K stack, because it will not fit into 4K?

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#9 lore raw
Quoted 8 lines · Pavel Machek
Quoted 7 lines · David Howells
Quoted 4 lines · Pavel Machek

The patch seems wrong to me; loader needs the stack, too, right?

What about making sure that the loader specifies reasonable stack size, instead?

Quoted 7 lines
Quoted 4 lines

The loader doesn't need its own stack, unless it _is_ the executable.  It uses the executable's stack.  The problem is that the executable and the loader can both specify the stack size for NOMMU, but which one do we pick, or do we consider both.

Imageine loader needs 64K, while executable only needs 4K. You surely want to execute it with 64K stack, because it will not fit into 4K?

i really dont think this is realistic. there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack. -mike

#10 lore raw
Quoted 14 lines · Mike Frysinger
Quoted 13 lines · Pavel Machek
Quoted 11 lines · David Howells
Quoted 4 lines · Pavel Machek

The patch seems wrong to me; loader needs the stack, too, right?

What about making sure that the loader specifies reasonable stack size, instead?

The loader doesn't need its own stack, unless it _is_ the executable.  It uses the executable's stack.  The problem is that the executable and the loader can both specify the stack size for NOMMU, but which one do we pick, or do we consider both.

Quoted 5 lines
Quoted 2 lines

Imageine loader needs 64K, while executable only needs 4K. You surely want to execute it with 64K stack, because it will not fit into 4K?

i really dont think this is realistic. there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

And if it _is_ realistic, the patch adds a bug.

=> patch is bad idea.
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#11 lore raw
Quoted 6 lines · Pavel Machek
Quoted 2 lines

i really dont think this is realistic. there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

And if it _is_ realistic, the patch adds a bug.

No, it doesn't. The problem is that the loader, when it is linked, is given a sillyly large default stack size, and this causes the application to be given a much larger stack than is strictly necessary - a stack that is drawn from a limited pool of non-pageable RAM and that must be allocated as a contiguous lump.

The executables should take into account the amount of stack space needed to call into all their libraries - and that includes the loader. You can argue that the loader should specify the amount of extra overhead it will need, and that this should be _added_ to the executable's stack estimate, but using the loader's stack estimate instead of the executable's does not really make sense.

You can also argue that the executable doesn't know how much space the loader will use, because the loader can be independently replaced; but this isn't a good argument because the loader doesn't know how much the executable will use, and neither know how much the various shared libs will use. The only way to do that is to add up all the stack estimates.

Now, it *is* perfectly reasonable to use the loader's stack estimate in the case that the loader is run as an executable. It could, for example, behave as the GLIBC loader and have some functions available for shared library query and suchlike.

David

#12 lore raw
Quoted 7 lines · David Howells
Quoted 5 lines · Pavel Machek
Quoted 2 lines

i really dont think this is realistic. there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

Quoted 7 lines
Quoted 1 line

And if it _is_ realistic, the patch adds a bug.

No, it doesn't. The problem is that the loader, when it is linked, is given a sillyly large default stack size, and this causes the application to be given a much larger stack than is strictly necessary - a stack that is drawn from a limited pool of non-pageable RAM and that must be allocated as a contiguous lump.

Fix the loader to only request as big stack as it needs?
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#13 lore raw
Quoted 9 lines · Pavel Machek
Quoted 8 lines · David Howells
Quoted 6 lines · Pavel Machek
Quoted 2 lines

i really dont think this is realistic.  there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

And if it _is_ realistic, the patch adds a bug.

Quoted 7 lines
Quoted 5 lines

No, it doesn't.  The problem is that the loader, when it is linked, is given a sillyly large default stack size, and this causes the application to be given a much larger stack than is strictly necessary - a stack that is drawn from a limited pool of non-pageable RAM and that must be allocated as a contiguous lump.

Fix the loader to only request as big stack as it needs?

and what if the loader needs a larger stack when run as an application ? you could make the same exact argument for every library that an application has a DT_NEEDED tag for, or that it dlopen()'s. but for the same reasons, it doesnt fly.

the only stack that should be checked is what the application itself says it needs. the ldso has no way of knowing what functions exactly the application in question will be using (whether in the ldso itself or in any library), thus only the application itself knows what the stack usage will look like.
-mike

#14 lore raw
Quoted 23 lines · Mike Frysinger
Quoted 9 lines · Pavel Machek
Quoted 8 lines · David Howells
Quoted 6 lines · Pavel Machek
Quoted 2 lines

i really dont think this is realistic.  there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

And if it _is_ realistic, the patch adds a bug.

Quoted 7 lines
Quoted 5 lines

No, it doesn't.  The problem is that the loader, when it is linked, is given a sillyly large default stack size, and this causes the application to be given a much larger stack than is strictly necessary - a stack that is drawn from a limited pool of non-pageable RAM and that must be allocated as a contiguous lump.

Fix the loader to only request as big stack as it needs?

and what if the loader needs a larger stack when run as an application ? you could make the same exact argument for every library that an application has a DT_NEEDED tag for, or that it dlopen()'s. but for the same reasons, it doesnt fly.

Quoted 5 lines

the only stack that should be checked is what the application itself says it needs. the ldso has no way of knowing what functions exactly the application in question will be using (whether in the ldso itself or in any library), thus only the application itself knows what the stack usage will look like.

And the application has no way of knowing how much stack this particular ldso needs. Too bad, it is all broken.

(And the solution of taking maximum of all involved parties still looks best to me.)
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#15 lore raw
Quoted 24 lines · Pavel Machek
Quoted 23 lines · Mike Frysinger
Quoted 9 lines · Pavel Machek
Quoted 8 lines · David Howells
Quoted 6 lines · Pavel Machek
Quoted 2 lines

i really dont think this is realistic.  there is exactly one ldso that everyone uses under FDPIC ELF, and it needs a very minuscule stack.

Not very realistic; but that argues that the patch is NOP.

And if it _is_ realistic, the patch adds a bug.

Quoted 7 lines
Quoted 5 lines

No, it doesn't.  The problem is that the loader, when it is linked, is given a sillyly large default stack size, and this causes the application to be given a much larger stack than is strictly necessary - a stack that is drawn from a limited pool of non-pageable RAM and that must be allocated as a contiguous lump.

Fix the loader to only request as big stack as it needs?

and what if the loader needs a larger stack when run as an application ?  you could make the same exact argument for every library that an application has a DT_NEEDED tag for, or that it dlopen()'s.  but for the same reasons, it doesnt fly.

Quoted 8 lines
Quoted 5 lines

the only stack that should be checked is what the application itself says it needs.  the ldso has no way of knowing what functions exactly the application in question will be using (whether in the ldso itself or in any library), thus only the application itself knows what the stack usage will look like.

And the application has no way of knowing how much stack this particular ldso needs. Too bad, it is all broken.

you're still wrongly assuming the ldso has any idea of what functions the application will be invoking. in the nommu embedded world (which is the *only* place this change matters), reduced stack sizes are not picked out of a hat. they're taken based on actual testing/review for a particular setup. along that same line, upgrading of complete systems (kernel/userspace/toolchain) arent dropped in casually -- this kind of reduced stack review would occur again. your concern is not realistic in any way nor applicable in any scenario that matters. -mike