From 0cf7fc4e115c2dcc40901c44b238ab36b5d966fc Mon Sep 17 00:00:00 2001 From: Zach Koopmans Date: Tue, 5 Feb 2019 10:00:22 -0800 Subject: Change /proc/PID/cmdline to read environment vector. - Change proc to return envp on overwrite of argv with limitations from upstream. - Add unit tests - Change layout of argv/envp on the stack so that end of argv is contiguous with beginning of envp. PiperOrigin-RevId: 232506107 Change-Id: I993880499ab2c1220f6dc456a922235c49304dec --- pkg/sentry/arch/stack.go | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'pkg/sentry/arch') diff --git a/pkg/sentry/arch/stack.go b/pkg/sentry/arch/stack.go index 716a3574d..f2cfb0426 100644 --- a/pkg/sentry/arch/stack.go +++ b/pkg/sentry/arch/stack.go @@ -170,6 +170,24 @@ func (s *Stack) Load(args []string, env []string, aux Auxv) (StackLayout, error) // Make sure we start with a 16-byte alignment. s.Align(16) + // Push the environment vector so the end of the argument vector is adjacent to + // the beginning of the environment vector. + // While the System V abi for x86_64 does not specify an ordering to the + // Information Block (the block holding the arg, env, and aux vectors), + // support features like setproctitle(3) naturally expect these segments + // to be in this order. See: https://www.uclibc.org/docs/psABI-x86_64.pdf + // page 29. + l.EnvvEnd = s.Bottom + envAddrs := make([]usermem.Addr, len(env)) + for i := len(env) - 1; i >= 0; i-- { + addr, err := s.Push(env[i]) + if err != nil { + return StackLayout{}, err + } + envAddrs[i] = addr + } + l.EnvvStart = s.Bottom + // Push our strings. l.ArgvEnd = s.Bottom argAddrs := make([]usermem.Addr, len(args)) @@ -182,18 +200,6 @@ func (s *Stack) Load(args []string, env []string, aux Auxv) (StackLayout, error) } l.ArgvStart = s.Bottom - // Push our environment. - l.EnvvEnd = s.Bottom - envAddrs := make([]usermem.Addr, len(env)) - for i := len(env) - 1; i >= 0; i-- { - addr, err := s.Push(env[i]) - if err != nil { - return StackLayout{}, err - } - envAddrs[i] = addr - } - l.EnvvStart = s.Bottom - // We need to align the arguments appropriately. // // We must finish on a 16-byte alignment, but we'll play it -- cgit v1.2.3