Skip to content

Commit d9154e9

Browse files
committed
Add containerenv information to /run/.containerenv
We have been asked to leak some information into the container to indicate: * The name and id of the container * The version of podman used to launch the container * The image name and ID the container is based on. * Whether the container engine is running in rootless mode. Fixes: containers#6192 Signed-off-by: Daniel J Walsh <[email protected]>
1 parent c675d8a commit d9154e9

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

docs/source/markdown/podman-run.1.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ Several files will be automatically created within the container. These include
2626
_/etc/hosts_, _/etc/hostname_, and _/etc/resolv.conf_ to manage networking.
2727
These will be based on the host's version of the files, though they can be
2828
customized with options (for example, **--dns** will override the host's DNS
29-
servers in the created _resolv.conf_). Additionally, an empty file is created in
30-
each container to indicate to programs they are running in a container. This file
31-
is located at _/run/.containerenv_.
29+
servers in the created _resolv.conf_). Additionally, a container environment
30+
file is created in each container to indicate to programs they are running in a
31+
container. This file is located at _/run/.containerenv_. When using the
32+
--privileged flag the .containerenv contains name/value pairs indicating the
33+
container engine version, whether the engine is running in rootless mode, the
34+
container name and id, as well as the image name and id that the container is based on.
3235

3336
When running from a user defined network namespace, the _/etc/netns/NSNAME/resolv.conf_
3437
will be used if it exists, otherwise _/etc/resolv.conf_ will be used.

libpod/container_internal_linux.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containers/podman/v2/pkg/rootless"
3636
"github.com/containers/podman/v2/pkg/util"
3737
"github.com/containers/podman/v2/utils"
38+
"github.com/containers/podman/v2/version"
3839
"github.com/containers/storage/pkg/archive"
3940
"github.com/containers/storage/pkg/idtools"
4041
securejoin "github.com/cyphar/filepath-securejoin"
@@ -1423,11 +1424,26 @@ func (c *Container) makeBindMounts() error {
14231424
}
14241425
}
14251426

1426-
// Make .containerenv
1427-
// Empty file, so no need to recreate if it exists
1427+
// Make .containerenv if it does not exist
14281428
if _, ok := c.state.BindMounts["/run/.containerenv"]; !ok {
1429-
// Empty string for now, but we may consider populating this later
1430-
containerenvPath, err := c.writeStringToRundir(".containerenv", "")
1429+
var containerenv string
1430+
isRootless := 0
1431+
if rootless.IsRootless() {
1432+
isRootless = 1
1433+
}
1434+
imageID, imageName := c.Image()
1435+
1436+
if c.Privileged() {
1437+
// Populate the .containerenv with container information
1438+
containerenv = fmt.Sprintf(`engine="podman-%s"
1439+
name=%q
1440+
id=%q
1441+
image=%q
1442+
imageid=%q
1443+
rootless=%d
1444+
`, version.Version.String(), c.Name(), c.ID(), imageName, imageID, isRootless)
1445+
}
1446+
containerenvPath, err := c.writeStringToRundir(".containerenv", containerenv)
14311447
if err != nil {
14321448
return errors.Wrapf(err, "error creating containerenv file for container %s", c.ID())
14331449
}

test/system/030-run.bats

+24
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,30 @@ json-file | f
536536
run_podman untag $IMAGE $newtag $newtag2
537537
}
538538

539+
@test "Verify /run/.containerenv exist" {
540+
run_podman run --rm $IMAGE ls -1 /run/.containerenv
541+
is "$output" "/run/.containerenv"
542+
543+
run_podman run --privileged --rm $IMAGE sh -c '. /run/.containerenv; echo $engine'
544+
is "$output" ".*podman.*" "failed to identify engine"
545+
546+
run_podman run --privileged --name "testcontainerenv" --rm $IMAGE sh -c '. /run/.containerenv; echo $name'
547+
is "$output" ".*testcontainerenv.*"
548+
549+
run_podman run --privileged --rm $IMAGE sh -c '. /run/.containerenv; echo $image'
550+
is "$output" ".*$IMAGE.*" "failed to idenitfy image"
551+
552+
run_podman run --privileged --rm $IMAGE sh -c '. /run/.containerenv; echo $rootless'
553+
# FIXME: on some CI systems, 'run --privileged' emits a spurious
554+
# warning line about dup devices. Ignore it.
555+
remove_same_dev_warning
556+
if is_rootless; then
557+
is "$output" "1"
558+
else
559+
is "$output" "0"
560+
fi
561+
}
562+
539563
@test "podman run with --net=host and --port prints warning" {
540564
rand=$(random_string 10)
541565

0 commit comments

Comments
 (0)