Skip to content

Commit cefe087

Browse files
committed
Fix argv rewrite on invoking hashbangless script (rhbz#1047506)
The fixargs() function is invoked when ksh needs to run a script without a #!/hashbang/path. Instead of letting the kernel invoke a shell, ksh exfile()s the script itself from sh_main(). In the forked child, it calls fixargs() to set the argument list in the environment to the args of the new script, so that 'ps' and /proc/PID/cmdline show the expected output. But fixargs() is broken because, on systems other than HP-UX (on which ksh uses pstat(2)), ksh simply inserts a terminating zero. The arguments list is not a zero-terminated C string. Unix systems expect the entire arguments buffer to be zeroed out, otherwise 'ps' and /proc/*/cmdline will have fragments of previous command lines in the output. The Red Hat patch for this bug is: https://src.fedoraproject.org/rpms/ksh/blob/642af4d6/f/ksh-20120801-argvfix.patch However, that fix is incomplete because 'command_len' was also hardcoded to be limited to 64 characters (!), which still gave invalid 'ps' output if the erased command line was longer. src/cmd/ksh93/sh/main.c: fixargs(): - Remove CMD_LENGTH macro which was defined as 64. - Remove code that limited the erasure of the arguments buffer to CMD_LENGTH characters. That code also had quite a dodgy strdup() call -- it copies arguments to the heap, but they are never freed (or even used), so it's a memory leak. Also, none of this is ever done if the length is calculated using pstat(2) on HP-UX, which is a clear indication that it's unnecessary. (I think this code block must have been some experiment they forgot to remove. One reason why I think so is that a 64 byte arguments limit never made sense, even in the 1980s when they wrote ksh on 80-column CRT displays. Another indication of this is that fixing it didn't require adding anything; the code to do the right thing was already there, it was just being overridden.) - Zero out the full arguments length as in the Red Hat patch. src/cmd/ksh93/tests/basic.sh: - Add test. It's sort of involved because 'ps' is one of the least portable commands in practice, in spite of standardisation.
1 parent 651bbd5 commit cefe087

4 files changed

Lines changed: 42 additions & 17 deletions

File tree

‎NEWS‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ For full details, see the git log at: https://github.com/ksh93/ksh
33

44
Any uppercase BUG_* names are modernish shell bug IDs.
55

6+
2020-09-25:
7+
8+
- When ksh invoked a shell script that does not have a leading
9+
#!/hashbang/path, 'ps' and /proc/<PID>/cmdline showed corrupted output if
10+
the new script's command line was shorter than that of the invoking script.
11+
This has been fixed by wiping the arguments buffer correctly.
12+
613
2020-09-24:
714

815
- An omission made it impossible to turn off brace expansion within command

‎src/cmd/ksh93/include/version.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717
* David Korn <dgk@research.att.com> *
1818
* *
1919
***********************************************************************/
20-
#define SH_RELEASE "93u+m 2020-09-24"
20+
#define SH_RELEASE "93u+m 2020-09-25"

‎src/cmd/ksh93/sh/main.c‎

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
# include <nc.h>
5050
#endif /* _hdr_nc */
5151

52-
#define CMD_LENGTH 64
53-
5452
/* These routines are referenced by this module */
5553
static void exfile(Shell_t*, Sfio_t*,int);
5654
static void chkmail(Shell_t *shp, char*);
@@ -340,6 +338,7 @@ int sh_main(int ac, char *av[], Shinit_f userinit)
340338
}
341339
else
342340
{
341+
/* beenhere > 0: We're in a forked child, about to execute a script without a hashbang path. */
343342
fdin = shp->infd;
344343
fixargs(shp->st.dolv,1);
345344
}
@@ -697,6 +696,13 @@ static void chkmail(Shell_t *shp, char *files)
697696
#if defined(_lib_fork) && !defined(_NEXT_SOURCE)
698697
/*
699698
* fix up command line for ps command
699+
*
700+
* This function is invoked when ksh needs to run a script without a
701+
* #!/hashbang/path. Instead of letting the kernel invoke a shell, ksh
702+
* exfile()s the script itself from sh_main(). In the forked child, it calls
703+
* fixargs() to set the argument list in the environment to the args of the
704+
* new script, so that 'ps' and /proc/PID/cmdline show the expected output.
705+
*
700706
* mode is 0 for initialization
701707
*/
702708
static void fixargs(char **argv, int mode)
@@ -727,19 +733,6 @@ static void fixargs(char **argv, int mode)
727733
buff = argv[0];
728734
while(cp = *argv++)
729735
command_len += strlen(cp)+1;
730-
if(environ && *environ==buff+command_len)
731-
{
732-
for(argv=environ; cp = *argv; cp++)
733-
{
734-
if(command_len > CMD_LENGTH)
735-
{
736-
command_len = CMD_LENGTH;
737-
break;
738-
}
739-
*argv++ = strdup(cp);
740-
command_len += strlen(cp)+1;
741-
}
742-
}
743736
command_len -= 1;
744737
return;
745738
}
@@ -754,7 +747,8 @@ static void fixargs(char **argv, int mode)
754747
offset += size;
755748
buff[offset++] = ' ';
756749
}
757-
buff[offset-1] = 0;
750+
offset--;
751+
memset(&buff[offset], 0, command_len - offset);
758752
# ifdef PSTAT
759753
un.pst_command = stakptr(0);
760754
pstat(PSTAT_SETCMD,un,0,0,0);

‎src/cmd/ksh93/tests/basic.sh‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,5 +692,29 @@ actual=$(exptest foo)
692692
[[ $actual == "$expect" ]] || err_exit 'Corruption of multibyte char following expansion of single-char name' \
693693
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
694694
695+
# ======
696+
# ksh didn't rewrite argv correctly (rhbz#1047506)
697+
# When running a script without a #! hashbang path, ksh attempts to replace argv with the arguments
698+
# of the script. However, fixargs() didn't wipe out the rest of previous arguments after the last
699+
# \0. This caused an erroneous record in /proc/<PID>/cmdline and the output of the ps command.
700+
if actual=$(UNIX95=1 ps -o args= -p "$$" 2>&1) # UNIX95=1 makes this work on HP-UX
701+
# Some 'ps' implementations add leading and/or trailing whitespace. Remove.
702+
while [[ $actual == [[:space:]]* ]]; do actual=${actual#?}; done
703+
while [[ $actual == *[[:space:]] ]]; do actual=${actual%?}; done
704+
[[ $actual == "$SHELL $0" ]] # this is how shtests invokes this script
705+
then expect='./atest 1 2'
706+
echo 'sleep 10; exit 0' >atest
707+
chmod 755 atest
708+
./atest 1 2 &
709+
actual=$(UNIX95=1 ps -o args= -p "$!")
710+
kill "$!"
711+
while [[ $actual == [[:space:]]* ]]; do actual=${actual#?}; done
712+
while [[ $actual == *[[:space:]] ]]; do actual=${actual%?}; done
713+
[[ $actual == "$expect" ]] || err_exit "ksh didn't rewrite argv correctly" \
714+
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
715+
else err_exit "warning: skipping argv rewrite test due to noncompliant 'ps' utility (got $(printf %q "$actual"))"
716+
let Errors--
717+
fi
718+
695719
# ======
696720
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)