Skip to content

Commit 24b686e

Browse files
Merge pull request #25244 from giuseppe/mount-fix-segfault
images: fix segfault when mounting without cap_sys_admin
2 parents 708b349 + 2f71072 commit 24b686e

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

pkg/domain/infra/abi/containers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"github.com/containers/podman/v5/pkg/specgenutil"
3636
"github.com/containers/podman/v5/pkg/util"
3737
"github.com/containers/storage"
38+
"github.com/containers/storage/pkg/unshare"
3839
"github.com/containers/storage/types"
3940
"github.com/hashicorp/go-multierror"
4041
"github.com/sirupsen/logrus"
@@ -1361,7 +1362,11 @@ func (ic *ContainerEngine) ContainerInit(ctx context.Context, namesOrIds []strin
13611362
}
13621363

13631364
func (ic *ContainerEngine) ContainerMount(ctx context.Context, nameOrIDs []string, options entities.ContainerMountOptions) ([]*entities.ContainerMountReport, error) {
1364-
if os.Geteuid() != 0 {
1365+
hasCapSysAdmin, err := unshare.HasCapSysAdmin()
1366+
if err != nil {
1367+
return nil, err
1368+
}
1369+
if os.Geteuid() != 0 || !hasCapSysAdmin {
13651370
if driver := ic.Libpod.StorageConfig().GraphDriverName; driver != "vfs" {
13661371
// Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
13671372
// of the mount command.

pkg/domain/infra/abi/images.go

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/containers/podman/v5/pkg/errorhandling"
3939
"github.com/containers/podman/v5/pkg/rootless"
4040
"github.com/containers/storage"
41+
"github.com/containers/storage/pkg/unshare"
4142
"github.com/containers/storage/types"
4243
"github.com/opencontainers/go-digest"
4344
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
@@ -157,6 +158,28 @@ func (ir *ImageEngine) Mount(ctx context.Context, nameOrIDs []string, opts entit
157158
listMountsOnly := false
158159
var images []*libimage.Image
159160
var err error
161+
162+
hasCapSysAdmin, err := unshare.HasCapSysAdmin()
163+
if err != nil {
164+
return nil, err
165+
}
166+
167+
if os.Geteuid() != 0 || !hasCapSysAdmin {
168+
if driver := ir.Libpod.StorageConfig().GraphDriverName; driver != "vfs" {
169+
// Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
170+
// of the mount command.
171+
return nil, fmt.Errorf("cannot mount using driver %s in rootless mode", driver)
172+
}
173+
174+
became, ret, err := rootless.BecomeRootInUserNS("")
175+
if err != nil {
176+
return nil, err
177+
}
178+
if became {
179+
os.Exit(ret)
180+
}
181+
}
182+
160183
switch {
161184
case opts.All && len(nameOrIDs) > 0:
162185
return nil, errors.New("cannot mix --all with images")
@@ -178,22 +201,6 @@ func (ir *ImageEngine) Mount(ctx context.Context, nameOrIDs []string, opts entit
178201
}
179202
}
180203

181-
if os.Geteuid() != 0 {
182-
if driver := ir.Libpod.StorageConfig().GraphDriverName; driver != "vfs" {
183-
// Do not allow to mount a graphdriver that is not vfs if we are creating the userns as part
184-
// of the mount command.
185-
return nil, fmt.Errorf("cannot mount using driver %s in rootless mode", driver)
186-
}
187-
188-
became, ret, err := rootless.BecomeRootInUserNS("")
189-
if err != nil {
190-
return nil, err
191-
}
192-
if became {
193-
os.Exit(ret)
194-
}
195-
}
196-
197204
mountReports := []*entities.ImageMountReport{}
198205
for _, i := range images {
199206
var mountPoint string

0 commit comments

Comments
 (0)