• [PATCH] Prevent Perl exec function from ever interpreting commands as s

    From Paul Wise@21:1/5 to All on Tue Jun 13 05:00:01 2023
    This means that the dpkg-architecure -c/--command option will no
    longer be able to cause the shell to interpret the command.

    The system/exec functions sometimes execute the command as shell,
    passing an indirect object as the first argument avoids that.

    The shell usage happens always on Windows and on other platforms only
    when there is one argument and it contains shell metacharacters.

    Fixes: commit 07c81f94aa64e9b6f148c5b5cb24461708feb2b5
    See-also: https://perldoc.perl.org/functions/exec.html
    ---
    scripts/dpkg-architecture.pl | 2 +-
    scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl | 2 +-
    2 files changed, 2 insertions(+), 2 deletions(-)

    diff --git a/scripts/dpkg-architecture.pl b/scripts/dpkg-architecture.pl
    index 11fb0bdbd..b9caabcf9 100755
    --- a/scripts/dpkg-architecture.pl
    +++ b/scripts/dpkg-architecture.pl
    @@ -380,7 +380,7 @@ if ($action eq 'list') {
    @ENV{keys %v} = values %v;
    ## no critic (TestingAndDebugging::ProhibitNoWarnings)
    no warnings qw(exec);
    - exec @ARGV or syserr(g_('unable to execute %s'), "@ARGV");
    + exec { $ARGV[0] } @ARGV or syserr(g_('unable to execute %s'), "@ARGV");
    } elsif ($action eq 'query') {
    print "$v{$req_variable_to_print}\n";
    } elsif ($action eq 'list-known') {
    diff --git a/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl b/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    index 89a1caf71..5081de48a 100755
    --- a/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    +++ b/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    @@ -22,4 +22,4 @@ while (<$nm>
  • From Guillem Jover@21:1/5 to Paul Wise on Tue Jun 13 12:00:01 2023
    Hi!

    Thanks for the patch!

    On Tue, 2023-06-13 at 10:54:09 +0800, Paul Wise wrote:
    This means that the dpkg-architecure -c/--command option will no
    longer be able to cause the shell to interpret the command.

    The system/exec functions sometimes execute the command as shell,
    passing an indirect object as the first argument avoids that.

    The shell usage happens always on Windows and on other platforms only
    when there is one argument and it contains shell metacharacters.

    Fixes: commit 07c81f94aa64e9b6f148c5b5cb24461708feb2b5
    See-also: https://perldoc.perl.org/functions/exec.html
    ---
    scripts/dpkg-architecture.pl | 2 +-
    scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl | 2 +-
    2 files changed, 2 insertions(+), 2 deletions(-)

    diff --git a/scripts/dpkg-architecture.pl b/scripts/dpkg-architecture.pl index 11fb0bdbd..b9caabcf9 100755
    --- a/scripts/dpkg-architecture.pl
    +++ b/scripts/dpkg-architecture.pl
    @@ -380,7 +380,7 @@ if ($action eq 'list') {
    @ENV{keys %v} = values %v;
    ## no critic (TestingAndDebugging::ProhibitNoWarnings)
    no warnings qw(exec);
    - exec @ARGV or syserr(g_('unable to execute %s'), "@ARGV");
    + exec { $ARGV[0] } @ARGV or syserr(g_('unable to execute %s'), "@ARGV");

    This change would break the current semantics for this option, as it
    is specified to take a "command-string". Perhaps the man page needs to
    be improved to make all this more clear. Where did you find this to be
    a problem? (If we'd really wanted to change the semantics that would
    require an explicit deprecation cycle, first try to catch affected usage
    and warn, then emit errors on those and use the new semantics, etc.,
    but it's not clear to me this would be the better option.)

    diff --git a/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl b/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    index 89a1caf71..5081de48a 100755
    --- a/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    +++ b/scripts/t/Dpkg_Shlibs/spacesyms-o-map.pl
    @@ -22,4 +22,4 @@ while (<$nm>) {
    close $nm;

    push @cmds, $input, $output;
    -exec @cmds;
    +exec { $cmds[0] } @cmds;

    While applying this would not harm, it should also not be needed as we
    always pass at leasth three three elements in @cmds. Well it might on
    Windows, but I don't think it is generally supported anyway.

    Thanks,
    Guillem

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Wise@21:1/5 to Guillem Jover on Wed Jun 14 04:00:01 2023
    On Tue, 2023-06-13 at 11:56 +0200, Guillem Jover wrote:

    This change would break the current semantics for this option, as it
    is specified to take a "command-string". Perhaps the man page needs to
    be improved to make all this more clear. Where did you find this to be
    a problem? (If we'd really wanted to change the semantics that would
    require an explicit deprecation cycle, first try to catch affected usage
    and warn, then emit errors on those and use the new semantics, etc.,
    but it's not clear to me this would be the better option.)

    I was writing this mail when I stumbled upon the behaviour:

    https://lists.debian.org/msgid-search/da17fe19bcc05ba1032e5d81112bb6715825a6a7.camel@debian.org

    Ever since writing an old blog post about shell metacharacter injection attacks, I am always looking for situations where shell metacharacter
    injection could lead to unwanted or unexpected behaviour and I vastly
    prefer consistent C-style exec behaviour where there is no chance of
    the shell being involved and messing around with the command. IMO the
    decision to use shell should always be on the user and require the user
    to explicitly call the shell and write a shell script (or use `sh -c`).

    https://bonedaddy.net/pabs3/log/2014/02/17/pid-preservation-society/

    So I think that at minimum, the current behaviour should be documented.

    Perhaps it should go through the deprecation cycle you mention too.

    While applying this would not harm, it should also not be needed as we
    always pass at leasth three three elements in @cmds. Well it might on Windows, but I don't think it is generally supported anyway.

    Yes I understood that, but I thought it would be best to change the
    style of exec usage to be consistent throughout dpkg. Dpkg::IPC uses
    this style, dpkg-architecture should and the whole codebase should too.

    --
    bye,
    pabs

    https://wiki.debian.org/PaulWise

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEEYQsotVz8/kXqG1Y7MRa6Xp/6aaMFAmSJHLEACgkQMRa6Xp/6 aaMCWhAAtUECxZ2MwHT0eaZ4TyAH/TBPXUtMfbkq1XNTy6MDAS7AMoAdD+VUJCH6 zwT26RUlfyKbvGJGXff4H7d7ufxjABHFq5LxBrJAiBsh8tFE7svTHNZFBNujWmjP zRp65T/f7PH8RIzTvMq4v2qRjAgcvGj3RGyqqpaZLHCQmdLJI902KQUEShUfrtbi PDXIOMxiJHqSlXEkorEbssz2/LQvUrHiQ4vq8zlgoqTf5f3JH2x6dbo5eaY7eHgo wNJ1Y0ZHG0DaRjlcsTT7GiyJBwzssFZAWeA8uPfgCtYxdhtR93uSULTzwefYyUx0 bnwwcKYPOnW/hlf2+dZNZMpUaWIMra9wnhyKSt41f/j7yOVypAyHu17y9YH52Zkw DcEP3eR5sRaufBen8AR2vJ5+I/qpqFH+telAyKVtjrWjpF6gzoxPPU1FmQvEQRqK VBmh3xsFIFIJYb7TZi6qhne77li0xYJOm87E7AZbSt8uT93hpgu6F3Xl4OsA5xTu yMOm+9U1QMEcpFvLNoKvRtTXdAZio61saQgaPnz3jU2HN4rZTuCri8zw8to7EYyh BB+qhZh+51+WYKQLQ8QM9NQUA3wJ/2X+4lq2ITTxabCCz9S++BHC2pVqkF38ps4M yc7UuTYAs8eBPpU+ZpQnJAgNWPGRtYP9fpR1CBwXVne4d85EJU4=
    =YQsM
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Guillem Jover@21:1/5 to Paul Wise on Tue Jul 4 22:50:01 2023
    Hi!

    On Wed, 2023-06-14 at 09:49:41 +0800, Paul Wise wrote:
    On Tue, 2023-06-13 at 11:56 +0200, Guillem Jover wrote:
    This change would break the current semantics for this option, as it
    is specified to take a "command-string". Perhaps the man page needs to
    be improved to make all this more clear. Where did you find this to be
    a problem? (If we'd really wanted to change the semantics that would require an explicit deprecation cycle, first try to catch affected usage and warn, then emit errors on those and use the new semantics, etc.,
    but it's not clear to me this would be the better option.)

    I was writing this mail when I stumbled upon the behaviour:

    https://lists.debian.org/msgid-search/da17fe19bcc05ba1032e5d81112bb6715825a6a7.camel@debian.org

    Ever since writing an old blog post about shell metacharacter injection attacks, I am always looking for situations where shell metacharacter injection could lead to unwanted or unexpected behaviour and I vastly
    prefer consistent C-style exec behaviour where there is no chance of
    the shell being involved and messing around with the command. IMO the decision to use shell should always be on the user and require the user
    to explicitly call the shell and write a shell script (or use `sh -c`).

    https://bonedaddy.net/pabs3/log/2014/02/17/pid-preservation-society/

    So I think that at minimum, the current behaviour should be documented.

    Attached is the man page update I've got queued locally, but I'm happy
    to clarify further if you think that would be insufficient.

    Perhaps it should go through the deprecation cycle you mention too.

    I checked on sources.d.n, and AFAIR only saw a couple of potentially problematic instances. But this might be affecting local scripts and
    tooling. I'll ponder about it, or perhaps ask on d-d or similar
    whether this would be very disruptive.

    While applying this would not harm, it should also not be needed as we always pass at leasth three three elements in @cmds. Well it might on Windows, but I don't think it is generally supported anyway.

    Yes I understood that, but I thought it would be best to change the
    style of exec usage to be consistent throughout dpkg. Dpkg::IPC uses
    this style, dpkg-architecture should and the whole codebase should too.

    Ack, I considered whether switching that script to use Dpkg::IPC would
    be better, but it does not depend on any other Dpkg module, and it is
    intended to be run from the build tree, so your change as-is looks good,
    and so I've queued that part too.

    And I found another instance in the dpkg-shlibdeps code which I've
    also fixed.

    I'm attaching the three patches. Thanks!

    Regards,
    Guillem

    From 68bd49c89adec124962a1fd654a488868ac08d00 Mon Sep 17 00:00:00 2001
    From: Guillem Jover <guillem@debian.org>
    Date: Thu, 15 Jun 2023 23:49:51 +0200
    Subject: [PATCH 1/3] man: Clarify dpkg-architecture -c option

    Prompted-by: Paul Wise <pabs@debian.org>
    ---
    man/dpkg-architecture.pod | 3 +++
    1 file changed, 3 insertions(+)

    diff --git a/man/dpkg-architecture.pod b/man/dpkg-architecture.pod
    index ea963db36..539f700d4 100644
    --- a/man/dpkg-architecture.pod
    +++ b/man/dpkg-architecture.pod
    @@ -93,6 +93,9 @@ Print a similar command to B<--print-set> but to unset all variables.
    Execute a I<command-string> in an environment which has all variables
    set to the determined value.

    +If the I<command-string> contains metacharacters, then it will be invoked +through the system bourne shell.
    +
    =item B<-L>, B<--list-known>

    Print a list of valid architecture names.
    --
    2.40.1


    From be1723de4efcad748e0c7cb0d0cef7c4efd7bcb3 Mon Sep 17 00:00:00 2001
    From: Paul Wise <pabs@debian.org>
    Date: Tue, 4 Jul 2023 19:57:57 +0200
    Subject: [PATCH 2/3] build: Avoid Perl's exec() falling back to system()

    The system/exec functions sometimes execute the command as shell,
    passing an indirect object as the first argument avoids that.

    The shell usage happe
  • From Paul Wise@21:1/5 to Guillem Jover on Wed Jul 5 08:00:01 2023
    On Tue, 2023-07-04 at 22:48 +0200, Guillem Jover wrote:

    Attached is the man page update I've got queued locally, but I'm happy
    to clarify further if you think that would be insufficient.

    Please write "shell metacharacters" instead of "metacharacters".

    On Wed, 2023-06-14 at 09:49:41 +0800, Paul Wise wrote:
    Perhaps it should go through the deprecation cycle you mention too.

    I checked on sources.d.n, and AFAIR only saw a couple of potentially problematic instances. But this might be affecting local scripts and
    tooling. I'll ponder about it, or perhaps ask on d-d or similar
    whether this would be very disruptive.

    Ack, seems reasonable.

    Ack, I considered whether switching that script to use Dpkg::IPC would
    be better, but it does not depend on any other Dpkg module, and it is intended to be run from the build tree, so your change as-is looks good,
    and so I've queued that part too.

    Ack, thanks.

    And I found another instance in the dpkg-shlibdeps code which I've
    also fixed.

    Woops, thanks.

    I'm attaching the three patches. Thanks!

    Looks good.

    --
    bye,
    pabs

    https://wiki.debian.org/PaulWise

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEEYQsotVz8/kXqG1Y7MRa6Xp/6aaMFAmSlBg4ACgkQMRa6Xp/6 aaO4rhAAnY3PP4HVEn0hCC9DuViPQOOb2oSSlIRDG4lW7uLEBfKXAvGrNDJVJ+P3 SnGmn2m6KX7hKkhPcaVWAS5K5QxzSjLZZPwuH4h7L6SjBGod7RU99FrxiHdR210C z78H9UP87t3HtbS3wQSIap6nr4s+dmjDv1XlKIcj+gG4WswB1Thrbu0GrcuEDXhS 21amzk8tuFNfSODJXyqAgaG3elr2nccl5E0lkp8ARw7CqRXUC5ahDSiDG0Y6n8/5 a6P5xI7TxaGhnjNSAGZuiucsSIQ89htnhQpGAaaayJ6ZxX1rpgYuAaTVNjstWPrk Ygj7mcTfuXfLEhBEI3ror2Ntx2C7MJW2NrOCjxHwL0wqjHr/fHMmTsX03Qr9XUlV FIlTBnzNRJ6R5NJQxtlIjiE8cLYmKJvZu4vmnoMxX+i9TH4WR5IUuOzlaZmTHgIh SZl64Zs5asTnWKKe4JpmN46U5VETwPFvzcdsY9KX8E/GX0+MfwdWe+x1MH73KHq6 +4ZQTZPq4qPDkF8hPBCBvUwVzsxbokSUmwQSLduk4+pE/mZ0Uw4oCPugety15wFQ k0jvjUZkZTrPWpg3g2KVky4Jqnl/ega1nGO+8fjc81X27h2ugWNPvWdGOjUpJuJw Vd2yZyRQ9WjpUltbscukflahQGPR6nK8AVxLpzymijhno+o7TWQ=
    =9YrS
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Guillem Jover@21:1/5 to Paul Wise on Wed Jul 5 09:20:01 2023
    Hi!

    On Wed, 2023-07-05 at 13:56:30 +0800, Paul Wise wrote:
    On Tue, 2023-07-04 at 22:48 +0200, Guillem Jover wrote:
    Attached is the man page update I've got queued locally, but I'm happy
    to clarify further if you think that would be insufficient.

    Please write "shell metacharacters" instead of "metacharacters".

    Ah, right, thanks! Changed locally.

    Regards,
    Guillem

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)