cben has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks,

I'm trying to debug a program that uses xargs to feed a file of hex addresses (one per line) to addr2line. The output is piped to an open command to be read in and used later.

I didn't write the program but after days of debugging, I realize I need some help.

Here's the subroutine in question:

sub src_line_info { # see below for values for $addr2line, $exec, $cmd my ($addr2line, $exec) = @_; my ($cmd) = "xargs $addr2line -e $exec"; my ($tmpfile) = tmpnam(); my (@a2loutput); open(ADDRS, ">$tmpfile") or die "open($tmpfile) failed: $!\n"; foreach my $ra (@trace_addrs) { print ADDRS "$ra->[TADDR_ADDRESS]\n"; } close(ADDRS); $cmd = "$cmd < $tmpfile |"; open(LINES, $cmd) or die "open($cmd) failed: $!\n"; while (<LINES>) { chomp(); push(@a2loutput, [split(/:/, $_)]); } close(LINES); . . . }

Variable values:
================

src_line_info: $addr2line=/auto/stbu-tools/wrlinux/wrl6/wrlinux-6/laye +rs/binary-toolchain-4.8-39/bin/i686-wrs-linux-gnu-addr2line<br> src_line_info: $exec=/auto/pix-asa-image/essen/9.4.2.11/smp<br> src_line_info: $cmd to open for LINES=xargs -p /auto/stbu-tools/wrlinu +x/wrl6/wrlinux-6/layers/binary-toolchain-4.8-39/bin/i686-wrs-linux-gn +u-addr2line -e /auto/pix-asa-image/essen/9.4.2.11/smp < /tmp/fileAnma +9v |

/tmpfile example data:
======================

0x0000000002823a32<br> 0x00000000007254dd<br> 0x00000000017199f0<br> 0x00000000017230fc<br> 0x000000000282397b<br> 0x00000000007254dd<br> 0x00000000017199f0<br> 0x00000000017230fc<br> 0x0000000000c88e63<br> 0x0000000000c93f7d<br> 0x0000000000c87aba<br>
expected <LINES> output of:
===========================

/local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/data-plane/dispatch/ +../../Xpix/../infrastructure/mp-datastruct/mp_percore.h:163<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/data-plane/dispatch/ +dispatch_lb.c:1636</p> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/infrastructure/lina/ +linux/lina.c:1592<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/infrastructure/lina/ +linux/lina_mem.c:255<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/data-plane/dispatch/ +dispatch_lb.c:1272<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/data-plane/dispatch/ +dispatch_lb.c:1636<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/infrastructure/lina/ +linux/lina.c:1592<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/infrastructure/lina/ +linux/lina_mem.c:255<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/Xpix/ixgbe_drv/ixgbe +_drv.c:377<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/Xpix/ixgbe_drv/ixgbe +_vport.c:61<br> /local/builders/essen/9.4.2_fcs_throttle/9.4.2.11/Xpix/ixgbe_drv/ixgbe +_drv.c:3001<br>
Actual output:
==============

sh: /sw/packages/findutils/4.1/bin/xargs: Permission denied

The problem seems to be that either the

"open(LINES, $cmd)"

or

 "while(<LINES>)"

is returning the following error message:

"sh: /sw/packages/findutils/4.1/bin/xargs: Permission denied"

I've verified that:
- addr2line is executable
- tmpfile is readable
- the $exec file is rwx
- xargs is -rwxr-xr-x. 1 4294967294 4294967294 64484 Oct 28 2003 /sw/packages/findutils/4.1/bin/xargs
- the open command error message is not output
- I can execute the command line in $cmd with out the "|" manually from the unix shell

I tried executing xargs with -p to make it prompt so I could see if there was a specific error message from the temp file that wasn't coming back, but it didn't work.

I've used different files to ultimately generate the tmpfile, so it isn't the tmpfile itself that is the problem.

I'm out of ideas. Any help is appreciated.

One final thing. This code is from a build tree and will be invoked mechanically by a tool, so I can't really modify it directly. I will have to find ways to modify the code it calls (addr2line/xargs versions, etc). But I can modify it to debug it and figure out what's going on if you have any ideas on that front.

Many thanks for taking a look!!

cben

Replies are listed 'Best First'.
Re: xargs error using open cmd for output with redirected input file
by hippo (Archbishop) on Nov 30, 2016 at 08:33 UTC
    the following error message: "sh: /sw/packages/findutils/4.1/bin/xargs: Permission denied"

    So that's the shell complaining, which means the problem is unlikely to be directly related to perl. You can either work out what's different between your successful invocation of the shell command interactively and when it is run from this script (uid, gid, euid, env, pwd, context, ...) or you can add diagnostic debugging statements into the script to tell you these things automatically, eg.

    system ("id; env");

    You haven't said (or I didn't spot) how you are running the resulting perl (interactive? at? cron?) or which OS you are using, so it's a bit hard to offer further advice at this stage.

      Thank you for your reply!

      So that's the shell complaining, which means the problem is unlikely to be directly related to perl.

      You're right, I just wasn't getting the implications of this.

      I was running the script manually as root (I know), whereas the automatic invocation is happening through a cgi program through a web server (both on a linux vm).

      I'd verified the uid/euid and gid/egid were the same using debug statements, but only for the manual invocation.

      I believe the cgi is running as Apache:Apache. I guess this could be a setuid problem but I'm not familiar with that at all. And I realize that's beyond the scope of a Perl forum (although any insight is welcome!).

      Again, many thanks!

      .
Re: xargs error using open cmd for output with redirected input file (setuid)
by Anonymous Monk on Nov 30, 2016 at 01:19 UTC

    Does $cmd work if you paste it into the shell?

    Nothing wrong with that subroutine aside from shell interpolation vulnerability, which wouldn't be triggered by these strings (no meta characters)

    src_line_info( '/auto/stbu-tools/wrlinux/wrl6/wrlinux-6/layers/binary-toolchain-4.8- +39/bin/i686-wrs-linux-gnu-addr2line', '/auto/pix-asa-image/essen/9.4.2.11/smp', );

    If $cmd works from shell but not from src_line_info, then other part of program must use setuid to change the user, or the user running the program isn't the same one as your shell

      Thank you for your reply!

      Does $cmd work if you paste it into the shell?

      Yes, it works from the shell, running it as root.

      the user running the program isn't the same one as your shell

      This is the case--when invoked automatically, it's a cgi program and I think the uid/gid is Apache/Apache (but I'll check). It's still not clear to me why with perms set to rwxr-xr-x for everything why it wouldn't work. But at least I think I''m on the right track now.

      Thanks again for your help!

      Chuck

Re: xargs error using open cmd for output with redirected input file
by Laurent_R (Canon) on Nov 30, 2016 at 07:26 UTC

      You are quite right. That was thoughtless on my part. I apologize and will do so in the future.