Jira (FACT-2345) Facter::Core::Execution.execute incorrectly expands shell builtins

12 views
Skip to first unread message

Oana Tanasoiu (JIRA)

unread,
Feb 4, 2020, 4:37:04 AM2/4/20
to puppe...@googlegroups.com
Oana Tanasoiu created an issue
 
Facter / Task FACT-2345
Facter::Core::Execution.execute incorrectly expands shell builtins
Issue Type: Task Task
Assignee: Unassigned
Created: 2020/02/04 1:36 AM
Priority: Normal Normal
Reporter: Oana Tanasoiu

When executing commands, Facter will expand the first word in the command string to be a fully qualified path. I.e. ls -l will become /usr/bin/ls -l. If the command string is a compound command that contains a pipeline or conditional construct, then Facter will wrap the string in /usr/bin/sh -c.

However, the first word of the compound is still expanded to an absolute path. This breaks shell builtins like cd as they are expanded to external commands like /usr/bin/cd or fail to be found on the PATH.

Reproduction Case

  • Install the latest version of puppet-agent on CentOS 7, along with strace:

yum install -y http://yum.puppetlabs.com/puppet-release-el-7.noarch.rpm
yum install -y puppet-agent strace

  • Create a test script that loads Facter, and uses it to execute a compound command that begins with cd:

cat <<EOF > test.rb
#!/opt/puppetlabs/puppet/bin/ruby
require 'facter'
 
puts Facter::Core::Execution.execute('cd /opt/puppetlabs && ls')
EOF
chmod +x test.rb

  • Execute the test script.

Outcome

The script prints the contents of the current working directory instead of /opt/puppetlabs:

# ./test.rb
1
anaconda-ks.cfg
linux.iso
test.rb

Running the script under strrace reveals that cd is being expanded to /usr/bin/cd before being passed to sh -c:

# strace -f -e trace=execve ./test.rb
execve("./test.rb", ["./test.rb"], [/* 23 vars */]) = 0
strace: Process 20373 attached
strace: Process 20374 attached
[pid 20374] execve("/usr/bin/sh", ["sh", "-c", "/usr/bin/cd /opt/puppetlabs && l"...], [/* 24 vars */]) = 0
strace: Process 20375 attached
[pid 20375] execve("/usr/bin/cd", ["/usr/bin/cd", "/opt/puppetlabs"], [/* 24 vars */]) = 0
[pid 20375] +++ exited with 0 +++
[pid 20374] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=20375, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
strace: Process 20376 attached
[pid 20376] execve("/usr/bin/ls", ["ls"], [/* 24 vars */]) = 0
[pid 20376] +++ exited with 0 +++
[pid 20374] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=20376, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
[pid 20374] +++ exited with 0 +++
[pid 20372] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=20374, si_uid=0, si_status=0, si_utime=0, si_stime=0} ---
1
anaconda-ks.cfg
linux.iso
test.rb
[pid 20373] +++ exited with 0 +++
+++ exited with 0 +++

Expected Outcome

The script prints the content of /opt/puppetlabs:

# ./test.rb
bin
facter
puppet
pxp-agent

Suggested Workaround

The expansion only affects the first word in the command line, so adding an extra true && to the compound command acts as a sacrificial noop that takes the hit instead.

Add Comment Add Comment
 
This message was sent by Atlassian JIRA (v7.7.1#77002-sha1:e75ca93)
Atlassian logo

Mihai Buzgau (Jira)

unread,
Aug 11, 2020, 8:24:05 AM8/11/20
to puppe...@googlegroups.com
Mihai Buzgau updated an issue
Change By: Mihai Buzgau
Sprint: ready for triage
This message was sent by Atlassian Jira (v8.5.2#805002-sha1:a66f935)
Atlassian logo

Bogdan Irimie (Jira)

unread,
Aug 19, 2020, 10:36:03 AM8/19/20
to puppe...@googlegroups.com
Bogdan Irimie updated an issue
Change By: Bogdan Irimie
Sprint: ready for triage ghost-26.08

Bogdan Irimie (Jira)

unread,
Aug 19, 2020, 10:37:03 AM8/19/20
to puppe...@googlegroups.com

Bogdan Irimie (Jira)

unread,
Aug 26, 2020, 9:04:04 AM8/26/20
to puppe...@googlegroups.com
Bogdan Irimie updated an issue
Change By: Bogdan Irimie
Sprint: ghost-26.08 , ready for triage 2

Florin Dragos (Jira)

unread,
Sep 2, 2020, 2:49:03 AM9/2/20
to puppe...@googlegroups.com

Bogdan Irimie (Jira)

unread,
Sep 2, 2020, 9:07:06 AM9/2/20
to puppe...@googlegroups.com
Bogdan Irimie updated an issue
Change By: Bogdan Irimie
Sprint: ghost-26.08, ghost-2.09 , ready for triage 2
Reply all
Reply to author
Forward
0 new messages