@@ -48,6 +48,8 @@ type runcJailer struct {
48
48
gid uint32
49
49
}
50
50
51
+ const firecrackerFileName = "firecracker"
52
+
51
53
func newRuncJailer (ctx context.Context , logger * logrus.Entry , ociBundlePath , runcBinPath string , uid , gid uint32 ) (* runcJailer , error ) {
52
54
l := logger .WithField ("ociBundlePath" , ociBundlePath ).
53
55
WithField ("runcBinaryPath" , runcBinPath )
@@ -129,7 +131,7 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
129
131
130
132
rootPathToConfig := filepath .Join (ociBundlePath , "config.json" )
131
133
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 {
133
135
return errors .Wrapf (err , "failed to copy config from %v to %v" , runcConfigPath , rootPathToConfig )
134
136
}
135
137
@@ -140,24 +142,16 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
140
142
141
143
// copy the firecracker binary
142
144
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
153
148
}
154
149
155
150
// copy the kernel image
156
151
newKernelImagePath := filepath .Join (rootPath , kernelImageFileName )
157
152
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
161
155
}
162
156
163
157
m .Cfg .KernelImagePath = kernelImageFileName
@@ -178,13 +172,12 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *Config, socketPath *string, vmI
178
172
defer f .Close ()
179
173
180
174
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
184
178
}
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
188
181
}
189
182
}
190
183
@@ -287,11 +280,7 @@ func (j runcJailer) ExposeFileToJail(srcPath string) error {
287
280
}
288
281
289
282
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 {
295
284
return err
296
285
}
297
286
@@ -302,6 +291,17 @@ func (j runcJailer) ExposeFileToJail(srcPath string) error {
302
291
return nil
303
292
}
304
293
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
+
305
305
// exposeBlockDeviceToJail will call mknod on the block device to ensure
306
306
// visibility of the device
307
307
func exposeBlockDeviceToJail (dst string , rdev , uid , gid int ) error {
@@ -387,7 +387,7 @@ func (j runcJailer) overwriteConfig(cfg *Config, socketPath, configPath string)
387
387
return err
388
388
}
389
389
390
- if err := ioutil .WriteFile (configPath , configBytes , 0444 ); err != nil {
390
+ if err := ioutil .WriteFile (configPath , configBytes , 0400 ); err != nil {
391
391
return err
392
392
}
393
393
@@ -403,7 +403,7 @@ func (j runcJailer) setDefaultConfigValues(cfg *Config, socketPath string, spec
403
403
404
404
if spec .Process .Args == nil {
405
405
cmd := firecracker.VMCommandBuilder {}.
406
- WithBin ("/firecracker" ).
406
+ WithBin ("/" + firecrackerFileName ).
407
407
WithSocketPath (socketPath ).
408
408
// Don't need to pass in an actual context here as we are only building
409
409
// the command arguments and not actually building a command
0 commit comments