Skip to content

Commit 1f8125e

Browse files
authored
Merge pull request #316 from kzys/runc-perms
Fine-tune runc-related permissions
2 parents 6c865e4 + 70c7a97 commit 1f8125e

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

runtime/runc_jailer.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ type runcJailer struct {
4848
gid uint32
4949
}
5050

51+
const firecrackerFileName = "firecracker"
52+
5153
func newRuncJailer(ctx context.Context, logger *logrus.Entry, ociBundlePath, runcBinPath string, uid, gid uint32) (*runcJailer, error) {
5254
l := logger.WithField("ociBundlePath", ociBundlePath).
5355
WithField("runcBinaryPath", runcBinPath)
@@ -129,7 +131,7 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
129131

130132
rootPathToConfig := filepath.Join(ociBundlePath, "config.json")
131133
j.logger.WithField("rootPathToConfig", rootPathToConfig).Debug("Copying config")
132-
if err := copyFile(runcConfigPath, rootPathToConfig, 0444); err != nil {
134+
if err := copyFile(runcConfigPath, rootPathToConfig, 0400); err != nil {
133135
return errors.Wrapf(err, "failed to copy config from %v to %v", runcConfigPath, rootPathToConfig)
134136
}
135137

@@ -140,24 +142,16 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
140142

141143
// copy the firecracker binary
142144
j.logger.WithField("root path", rootPath).Debug("copying firecracker binary")
143-
newFirecrackerBinPath := filepath.Join(rootPath, filepath.Base(cfg.FirecrackerBinaryPath))
144-
if err := copyFile(
145-
cfg.FirecrackerBinaryPath,
146-
newFirecrackerBinPath,
147-
0500,
148-
); err != nil {
149-
return errors.Wrapf(err, "could not copy firecracker binary from path %v", cfg.FirecrackerBinaryPath)
150-
}
151-
if err := os.Chown(newFirecrackerBinPath, int(j.uid), int(j.gid)); err != nil {
152-
return errors.Wrap(err, "failed to change ownership of binary")
145+
newFirecrackerBinPath := filepath.Join(rootPath, firecrackerFileName)
146+
if err := j.copyFileToJail(cfg.FirecrackerBinaryPath, newFirecrackerBinPath, 0500); err != nil {
147+
return err
153148
}
154149

155150
// copy the kernel image
156151
newKernelImagePath := filepath.Join(rootPath, kernelImageFileName)
157152
j.logger.WithField("newKernelImagePath", newKernelImagePath).Debug("copying kernel image")
158-
159-
if err := copyFile(m.Cfg.KernelImagePath, newKernelImagePath, 0444); err != nil {
160-
return errors.Wrap(err, "failed to mount kernel image")
153+
if err := j.copyFileToJail(m.Cfg.KernelImagePath, newKernelImagePath, 0400); err != nil {
154+
return err
161155
}
162156

163157
m.Cfg.KernelImagePath = kernelImageFileName
@@ -178,13 +172,12 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
178172
defer f.Close()
179173

180174
if !internal.IsStubDrive(f) {
181-
info, err := os.Stat(drivePath)
182-
if err != nil {
183-
return errors.Wrapf(err, "failed to stat drive %q", drivePath)
175+
mode := 0600
176+
if firecracker.BoolValue(d.IsReadOnly) {
177+
mode = 0400
184178
}
185-
186-
if err := copyFile(drivePath, newDrivePath, info.Mode()); err != nil {
187-
return errors.Wrapf(err, "failed to copy drive %v", drivePath)
179+
if err := j.copyFileToJail(drivePath, newDrivePath, os.FileMode(mode)); err != nil {
180+
return err
188181
}
189182
}
190183

@@ -287,11 +280,7 @@ func (j runcJailer) ExposeFileToJail(srcPath string) error {
287280
}
288281

289282
dst := filepath.Join(parentDir, filepath.Base(srcPath))
290-
if err := copyFile(srcPath, dst, os.FileMode(stat.Mode)); err != nil {
291-
return err
292-
}
293-
294-
if err := os.Chown(dst, int(uid), int(gid)); err != nil {
283+
if err := j.copyFileToJail(srcPath, dst, os.FileMode(stat.Mode)); err != nil {
295284
return err
296285
}
297286

@@ -302,6 +291,17 @@ func (j runcJailer) ExposeFileToJail(srcPath string) error {
302291
return nil
303292
}
304293

294+
// copyFileToJail will copy a file from src to dst, and chown the new file to the jail user.
295+
func (j runcJailer) copyFileToJail(src, dst string, mode os.FileMode) error {
296+
if err := copyFile(src, dst, mode); err != nil {
297+
return err
298+
}
299+
if err := os.Chown(dst, int(j.uid), int(j.gid)); err != nil {
300+
return err
301+
}
302+
return nil
303+
}
304+
305305
// exposeBlockDeviceToJail will call mknod on the block device to ensure
306306
// visibility of the device
307307
func exposeBlockDeviceToJail(dst string, rdev, uid, gid int) error {
@@ -387,7 +387,7 @@ func (j runcJailer) overwriteConfig(cfg *Config, socketPath, configPath string)
387387
return err
388388
}
389389

390-
if err := ioutil.WriteFile(configPath, configBytes, 0444); err != nil {
390+
if err := ioutil.WriteFile(configPath, configBytes, 0400); err != nil {
391391
return err
392392
}
393393

@@ -403,7 +403,7 @@ func (j runcJailer) setDefaultConfigValues(cfg *Config, socketPath string, spec
403403

404404
if spec.Process.Args == nil {
405405
cmd := firecracker.VMCommandBuilder{}.
406-
WithBin("/firecracker").
406+
WithBin("/" + firecrackerFileName).
407407
WithSocketPath(socketPath).
408408
// Don't need to pass in an actual context here as we are only building
409409
// the command arguments and not actually building a command

runtime/runc_jailer_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestBuildJailedRootHandler_Isolated(t *testing.T) {
7373
{
7474
PathOnHost: firecracker.String(rootDrivePath),
7575
IsRootDevice: firecracker.Bool(true),
76+
IsReadOnly: firecracker.Bool(true),
7677
},
7778
},
7879
},

0 commit comments

Comments
 (0)