Bug 1714267

Summary: On RHEL8, when using a CSH script through a systemd unit, the user's env .cshrc is not read anymore
Product: Red Hat Enterprise Linux 8 Reporter: Christophe Besson <cbesson>
Component: setupAssignee: Pavel Zhukov <pzhukov>
Status: CLOSED ERRATA QA Contact: Petr Dancak <pdancak>
Severity: high Docs Contact: Katerina Nemcova <knemcova>
Priority: urgent    
Version: 8.0CC: afox, alexander.hass, bfinger, cbuissar, fkrska, lagordon, msekleta, pdancak, pdwyer, psklenar, rmetrich, stuart.harber, systemd-maint-list, thozza, toneata
Target Milestone: rcKeywords: Patch, Reproducer, TestCaseProvided, ZStream
Target Release: 8.0   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: setup-2.12.2-4.el8 Doc Type: No Doc Update
Doc Text:
Story Points: ---
Clone Of:
: 1755360 1755743 (view as bug list) Environment:
Last Closed: 2020-04-28 16:39:59 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1743192, 1755139, 1755360, 1755743    
Attachments:
Description Flags
strace of systemd with CSH script
none
strace of systemd with BASH script
none
patch v1 none

Description Christophe Besson 2019-05-27 14:24:18 UTC
Created attachment 1573960 [details]
strace of systemd with CSH script

Description of problem:
On RHEL8, when using a CSH script through a systemd unit, the user's env ~cshuser/.cshrc is not read anymore.
-> it is OK when the CSH script is run from the cmdline, with or without runuser.
-> this issue doesn't happen in using a BASH script through systemd
-> the behavior has changed since RHEL7.6 (CSH env file was sourced)

Version-Release number of selected component (if applicable):
systemd-239-13.el8_0.3

How reproducible:
Here is a dummy service for the "cshuser":
[Unit]
Description=dummy.service

[Service]
Type=forking
ExecStart=/usr/sbin/runuser - cshuser -c /usr/bin/dummy.sh

[Install]
WantedBy=multi-user.target



Steps to Reproduce:
=================== csh user ===================

# getent passwd cshuser
cshuser:x:1001:1001::/home/cshuser:/usr/bin/csh

# tail -n1 ~cshuser/.cshrc 
setenv TOTO 1

# systemctl start dummy
# strace -ttTvf -e %file,clone -s 4096 -o /tmp/trace-dummy-csh -p 1

# egrep "dummy.sh|shrc" /tmp/trace-dummy-csh 
23336 09:47:13.947847 execve("/usr/sbin/runuser", ["/usr/sbin/runuser", "-", "cshuser", "-c", "/usr/bin/dummy.sh"], ["LANG=en_US.UTF-8", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", "INVOCATION_ID=2f33063a9afc4838ab8e88c65f65c27e", "JOURNAL_STREAM=9:557418"]) = 0 <0.001992>
23356 09:47:15.658918 execve("/usr/bin/csh", ["-csh", "-c", "/usr/bin/dummy.sh"], ["HOME=/home/cshuser", "SHELL=/usr/bin/csh", "USER=cshuser", "LOGNAME=cshuser", "PATH=/usr/local/bin:/usr/bin", "XDG_SESSION_ID=c36", "XDG_RUNTIME_DIR=/run/user/1001", "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1001/bus"]) = 0 <0.000569>
23356 09:47:15.699224 openat(AT_FDCWD, "/etc/csh.cshrc", O_RDONLY) = 3 <0.000018>
23397 09:47:15.922735 execve("/usr/bin/dummy.sh", ["/usr/bin/dummy.sh"], ["HOME=/home/cshuser", "SHELL=/usr/bin/csh", "USER=cshuser", "LOGNAME=cshuser", "PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin", "XDG_SESSION_ID=c36", "XDG_RUNTIME_DIR=/run/user/1001", "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1001/bus", "HOSTTYPE=x86_64-linux", "VENDOR=unknown", "OSTYPE=linux", "MACHTYPE=x86_64", "SHLVL=1", "PWD=/home/cshuser", "GROUP=cshuser", "HOST=test8", "MAIL=/var/spool/mail/cshuser", "HOSTNAME=test8", "LANG=en_US.UTF-8"] <unfinished ...>
23397 09:47:15.934109 openat(AT_FDCWD, "/usr/bin/dummy.sh", O_RDONLY <unfinished ...>
23397 09:47:15.943649 openat(AT_FDCWD, "/etc/csh.cshrc", O_RDONLY) = 3 <0.000796>


=================== bash user ===================

# getent passwd user1
user1:x:1002:1002::/home/user1:/bin/bash

# tail -n1 ~user1/.bashrc
export TOTO=1

# systemctl start dummy
# strace -ttTvf -e %file,clone -s 4096 -o /tmp/trace-dummy-bash -p 1

# egrep "dummy.sh|shrc" /tmp/trace-dummy-bash
23596 09:56:51.626303 execve("/usr/sbin/runuser", ["/usr/sbin/runuser", "-", "user1", "-c", "/usr/bin/dummy.sh"], ["LANG=en_US.UTF-8", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin", "INVOCATION_ID=55baac4063f948b688df5196c3d0aee7", "JOURNAL_STREAM=9:561162"]) = 0 <0.001527>
23615 09:56:53.369473 execve("/bin/bash", ["-bash", "-c", "/usr/bin/dummy.sh"], ["HOME=/home/user1", "SHELL=/bin/bash", "USER=user1", "LOGNAME=user1", "PATH=/usr/local/bin:/usr/bin", "XDG_SESSION_ID=c37", "XDG_RUNTIME_DIR=/run/user/1002", "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1002/bus"]) = 0 <0.000465>
23615 09:56:53.559378 stat("/etc/bashrc", {st_dev=makedev(253, 0), st_ino=4265654, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=3001, st_atime=1558943347 /* 2019-05-27T03:49:07.075746762-0400 */, st_atime_nsec=75746762, st_mtime=1536580263 /* 2018-09-10T07:51:03-0400 */, st_mtime_nsec=0, st_ctime=1557736649 /* 2019-05-13T04:37:29.305867591-0400 */, st_ctime_nsec=305867591}) = 0 <0.000517>
23615 09:56:53.559984 openat(AT_FDCWD, "/etc/bashrc", O_RDONLY) = 4 <0.000012>
23615 09:56:53.562860 stat("/home/user1/.bashrc", {st_dev=makedev(253, 0), st_ino=10247510, st_mode=S_IFREG|0644, st_nlink=1, st_uid=1002, st_gid=1002, st_blksize=4096, st_blocks=8, st_size=326, st_atime=1558957093 /* 2019-05-27T07:38:13.686321444-0400 */, st_atime_nsec=686321444, st_mtime=1558957082 /* 2019-05-27T07:38:02.328094010-0400 */, st_mtime_nsec=328094010, st_ctime=1558957082 /* 2019-05-27T07:38:02.351094470-0400 */, st_ctime_nsec=351094470}) = 0 <0.000453>
23615 09:56:53.563428 openat(AT_FDCWD, "/home/user1/.bashrc", O_RDONLY) = 4 <0.000021>
23615 09:56:53.569858 stat("/etc/bashrc", {st_dev=makedev(253, 0), st_ino=4265654, st_mode=S_IFREG|0644, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=3001, st_atime=1558943347 /* 2019-05-27T03:49:07.075746762-0400 */, st_atime_nsec=75746762, st_mtime=1536580263 /* 2018-09-10T07:51:03-0400 */, st_mtime_nsec=0, st_ctime=1557736649 /* 2019-05-13T04:37:29.305867591-0400 */, st_ctime_nsec=305867591}) = 0 <0.000335>
23615 09:56:53.570302 openat(AT_FDCWD, "/etc/bashrc", O_RDONLY) = 4 <0.000031>
23615 09:56:53.573254 execve("/usr/bin/dummy.sh", ["/usr/bin/dummy.sh"], ["LANG=en_US.UTF-8", "HISTCONTROL=ignoredups", "HOSTNAME=test8", "TOTO=1", "XDG_SESSION_ID=c37", "USER=user1", "PWD=/home/user1", "HOME=/home/user1", "XDG_DATA_DIRS=/home/user1/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share", "MAIL=/var/spool/mail/user1", "SHELL=/bin/bash", "SHLVL=1", "LOGNAME=user1", "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1002/bus", "XDG_RUNTIME_DIR=/run/user/1002", "PATH=/home/user1/.local/bin:/home/user1/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin", "HISTSIZE=1000", "LESSOPEN=||/usr/bin/lesspipe.sh %s", "_=/usr/bin/dummy.sh"]) = 0 <0.000515>
23615 09:56:53.577229 openat(AT_FDCWD, "/usr/bin/dummy.sh", O_RDONLY) = 3 <0.000007>
23615 09:56:53.577257 stat("/usr/bin/dummy.sh", {st_dev=makedev(253, 0), st_ino=10247523, st_mode=S_IFREG|0755, st_nlink=1, st_uid=0, st_gid=0, st_blksize=4096, st_blocks=8, st_size=41, st_atime=1558965413 /* 2019-05-27T09:56:53.572461810-0400 */, st_atime_nsec=572461810, st_mtime=1558965208 /* 2019-05-27T09:53:28.289357578-0400 */, st_mtime_nsec=289357578, st_ctime=1558965208 /* 2019-05-27T09:53:28.307357939-0400 */, st_ctime_nsec=307357939}) = 0 <0.000005>

Actual results:


Expected results:


Additional info:

Comment 1 Christophe Besson 2019-05-27 14:25:30 UTC
Created attachment 1573961 [details]
strace of systemd with BASH script

Comment 3 Michal Sekletar 2019-05-28 17:14:45 UTC
Hmm...execve() for runuser looks identical for both bash and systemd. Anything after this point is either a bug in runuser or csh. Let's start with C-shell first.

Comment 4 Siteshwar Vashisht 2019-06-04 14:29:59 UTC
This issue is reproducible with tcsh 6.18.01 (version of tcsh from RHEL 7) on RHEL 8. Moving to util-linux for further investigation.

Comment 8 Christophe Besson 2019-06-08 08:27:36 UTC
Hi,

I had a look into the C-shell code, and I tried to force the source of ~/.cshrc a few lines after some conditions which seems to be not met (!fast && reenter == 0). And then, this *workaround* works for me (only for the first/original issue since I didn't succeed to reproduce the 2nd one which looks similar). 

I don't know if that proves something or if that can help to move forward the diagnostic.

From 2f9fd7782a71dd4c8e0b5558c4dd947cef825724 Mon Sep 17 00:00:00 2001
From: rpm-build <rpm-build>
Date: Fri, 7 Jun 2019 17:29:51 +0200
Subject: [PATCH] workaround: always source .cshrc

---
 sh.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/sh.c b/sh.c
index 09422e7..3daeae6 100644
--- a/sh.c
+++ b/sh.c
@@ -1372,6 +1372,10 @@ main(int argc, char **argv)
 	if (loginsh || rdirs)
 	    loaddirs(NULL);
     }
+
+    /* Always source ~/.cshrc */
+    (void) srccat(varval(STRhome), STRsldotcshrc);
+
     /* Reset interrupt flag */
     setintr = osetintr;
     parintr = oparintr;
-- 
1.8.3.1

Comment 9 Siteshwar Vashisht 2019-06-11 13:39:17 UTC
tcsh has an error while processing this line in `/etc/profile.d/lang.csh` script:

if (${?LANG} && ${TERM} == 'linux' && in_console == 0) then

This causes an error as variable TERM is not defined.

Reassigning to `setup` pacakage.

Comment 10 Martin Osvald 🛹 2019-08-13 14:15:34 UTC
Created attachment 1603391 [details]
patch v1

when looking at the code for lang.csh I can see several problems:

- no check whether the 'TERM' variable is set/defined - tcsh prematurely ends and prints 'Undefined variable' if it accesses undefined one
- missing/forgotten '$' before 'in_console' variable

~~~
-if (${?LANG} && ${TERM} == 'linux' && in_console == 0) then
~~~

vs

~~~
+if (${?LANG} && ${?TERM}) then
+    if (${TERM} == 'linux' && $in_console == 0) then
+        set utf8_used=`echo ${LANG} | grep --quiet -E -i -e '^.+\.utf-?8$'; echo $?`
~~~

- extra 'm' before 'case' statement

~~~
-            m case ar*:
~~~

vs

~~~
+                case ar*:
~~~

and possibly more bugs from the below dkaspar's disastrous commit which unfortunately got from fedora to rhel8:

https://github.com/fedora-sysv/initscripts/commit/59928ce7

The whole lang.csh needs to be reviewed.

Comment 41 errata-xmlrpc 2020-04-28 16:39:59 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2020:1770