help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Process substitution documentation request


From: Zachary Santer
Subject: Re: Process substitution documentation request
Date: Thu, 25 Jun 2026 02:15:22 -0400

On Wed, Jun 24, 2026 at 2:38 PM Borden via <help-bash@gnu.org> wrote:
>
> > The original command used a combination of cryptsetup and gpg, IIRC. I 
> > would guess that the gpg part needs to run as the non-privileged user.
>
> You're correct. sudo gpg breaks because sudo switches the context (I don't 
> remember the technical term) to root, so it goes looking through /root/.gnupg 
> instead of ${HOME}/.gnupg for a matching key.

It's still looking in "${HOME}/.gnupg". It's just that HOME got set to
/root first.

I don't have root access on any *nix system at the moment, so I can't
test any of this, but

1. If you're already root, does `sudo --user` still ask for the
specified user's password?

```
sudo -- bash -c "
  cryptsetup luksAddKey --key-file=<(
    sudo --user=\"${USER}\" -- gpg --decrypt \"${existing_key}\"
  ) \
  --new-keyfile=<(
    sudo --user=\"${USER}\" -- gpg --decrypt \"${new_keyfile}\"
  ) \"${device}\""
```

The expansions of ${USER} there are happening before the outermost
sudo call runs, so while you're still in your regular account.

2. If that doesn't work, you could try using sudo's --preserve-env
option, though that involves security policy stuff I'm ignorant of. If
you're allowed, though, the root user running the command would still
have your HOME environment variable, I believe.

```
sudo --preserve-env -- bash -c "
  cryptsetup luksAddKey --key-file=<(
    gpg --decrypt \"${existing_key}\"
  ) \
  --new-keyfile=<(
    gpg --decrypt \"${new_keyfile}\"
  ) \"${device}\""
```

3. Did you ever try my explicit named pipes thing from [1]? If that
didn't work, that's a pretty strong indication that cryptsetup can't
handle these arguments being pipes, regardless of what sudo is doing.

> > I don't know whether this would work with cryptsetup.  If you never tested 
> > cryptsetup with procsubs *without* sudo, then you don't know either.
>
> Doesn't work for me. I get blocking errors where gpg refuses to continue 
> until cryptsetup starts reading.

cryptsetup should still start reading at some point. There's no reason
gpg and cryptsetup would be deadlocked, each waiting on the other at
the same time.

On Wed, Jun 24, 2026 at 7:01 PM Borden via <help-bash@gnu.org> wrote:
>
> Makes more sense to me than setting --close-from to 100

But that actually worked or not?

> If they really want to be helpful, they can tweak sudo so that it closes all 
> *but* the specified file descriptors.

I was kind of trying to game out how something like this could be made
to work earlier, and I'd like some input from other people on this
list.

It's a pretty safe bet that when you're expanding two process
substitutions, and you haven't expanded any before, you're going to
get file descriptors numbered 63 and 62, but just hard-coding those
numbers in seems like really bad practice.

```
diff --unified=0 -- <(
  printf '%s\n' {1..5}
) \
<(
  printf '%s\n' {2..7}
)
```
--- /dev/fd/63  2026-06-24 22:18:37.000000000 -0400
+++ /dev/fd/62  2026-06-24 22:18:37.000000000 -0400
@@ -1 +0,0 @@
-1
@@ -5,0 +5,2 @@
+6
+7

Works just fine on my system.

```
declare -a -i procsub_fd=()

diff --unified=0 -- "${|
  REPLY=<(
    printf '%s\n' {1..5}
  )
  if [[ ${REPLY} == '/dev/fd/'* ]] then
    procsub_fd+=( "${REPLY#'/dev/fd/'}" )
  fi
}" \
"${|
  REPLY=<(
    printf '%s\n' {2..7}
  )
  if [[ ${REPLY} == '/dev/fd/'* ]] then
    procsub_fd+=( "${REPLY#'/dev/fd/'}" )
  fi
}"

printf '%s\n' "${procsub_fd[@]/#/--leave-open=}"
```
diff: /dev/fd/63: No such file or directory
--leave-open=63
--leave-open=63

This, not so much. Two problems here:

1. The lifetime of each procsub is only as long as it takes to assign
its expanded value to REPLY, not the lifetime of the command that the
enclosing funsub is constituent to. This is probably the right way for
this to work, regardless of what I'm trying to accomplish here.

2. Even if point 1. weren't the case, everything expands left to
right, and there's no way to feed sudo --leave-open arguments after
the arguments for the command sudo is being used to run. So there's no
way to have procsubs in that command and tell sudo to leave open only
those specific fds.

```
procsub_fd=()

exec {procsub_fd[0]}< <(
  printf '%s\n' {1..5}
)
exec {procsub_fd[1]}< <(
  printf '%s\n' {2..7}
)

printf '%s\n' "${procsub_fd[@]/#/--leave-open=}"

diff --unified=0 -- "${procsub_fd[@]/#/'/dev/fd/'}"
```
--leave-open=10
--leave-open=11
diff: /dev/fd/10: No such file or directory
diff: /dev/fd/11: No such file or directory

At least here, I know what the relevant fds are when I need to.
However, whether or not diff can see and act on the /dev/fd/n files is
evidently dependent on your operating system.[3] This definitely
doesn't work in MSYS2 MSYS, which runs inside of Windows.

--close-from=100 might be the best way to go, if figuring out the
specific fds that you don't want sudo to close, when you need to know,
is at best not something that can be done everywhere.

[1] https://lists.gnu.org/archive/html/help-bash/2026-06/msg00015.html
[2] https://lists.gnu.org/archive/html/help-bash/2026-06/msg00010.html
[3] https://lists.gnu.org/archive/html/bug-bash/2024-05/msg00371.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]