Bug 1885399 - stack overflow if cannot unlink $HOME/.sh_history
Summary: stack overflow if cannot unlink $HOME/.sh_history
Keywords:
Status: NEW
Alias: None
Product: Red Hat Enterprise Linux 7
Classification: Red Hat
Component: ksh
Version: 7.9
Hardware: All
OS: Linux
medium
medium
Target Milestone: rc
: ---
Assignee: Vincent Mihalkovič
QA Contact: Karel Volný
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2020-10-05 19:45 UTC by Paulo Andrade
Modified: 2021-10-01 14:21 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed:
Target Upstream Version:


Attachments (Terms of Use)

Description Paulo Andrade 2020-10-05 19:45:16 UTC
It will also fail the rename in the chunk:

src/cmd/ksh93/edit/history.c
"""
	unlink(hist_old->histname);
	if(access(hist_old->histname,F_OK) >= 0)
	{
		/* The unlink can fail on windows 95 */
		int fd;
		char *last, *name=hist_old->histname;
		close(sffileno(hist_old->histfp));
		tmpname = (char*)malloc(strlen(name)+14);
		if(last = strrchr(name,'/'))
		{
			*last = 0;
			pathtmp(tmpname,name,"hist",NIL(int*));
			*last = '/';
		}
		else
			pathtmp(tmpname,".","hist",NIL(int*));
		if(rename(name,tmpname) < 0)
		{
			free(tmpname);
			tmpname = name;
		}
		fd = open(tmpname,O_RDONLY);
		sfsetfd(hist_old->histfp,fd);
		if(tmpname==name)
			tmpname = 0;
	}
	hist_ptr = 0;
	if(fstat(sffileno(hist_old->histfp),&statb)>=0)
	{
		histinit = 1;
		histmode =  statb.st_mode;
	}
"""

  Another condition required is to have a full .sh_history.
This will cause sh_histinit to call hist_trim, that will
again call sh_histinit.

  On the backtrace, the condition is verified in the statb
local variable of hist_trim, where the call trace shows that
the unlink and rename syscalls failed.
  The unlink call may fail due to a stale file or some filesystem
issue. The rename call will fail if tmpname is on another filesystem;
could be nfs vs local filesystem or local filesystem and a tmpfs /tmp.

  The issue only happens if the unlink fails, but still, could use
a smarter or fallback approach to not cause the stack overflow crash.

Comment 4 Paulo Andrade 2020-10-27 19:18:40 UTC
We just had another case with a slightly different condition for this to happen.

Still an erroneous state. Basically, needs a .sh_history that will cause hist_trim
to be called, and not have write permissions to $HOME.

The other recent cases were stale nfs file and an issue with a local filesystem.


Note You need to log in before you can comment on or make changes to this bug.