Skip to content

Commit 5a89577

Browse files
authored
Merge pull request #453 from kzys/log-rename
Rename the files inside a jail to avoid conflicts
2 parents fc78a26 + 8a93439 commit 5a89577

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

runtime/runc_jailer.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,14 @@ func (j *runcJailer) BuildJailedRootHandler(cfg *config.Config, machineConfig *f
270270
}
271271
}
272272

273-
func (j *runcJailer) makeLinkInJail(src string) (string, error) {
273+
// makeLinkInJail creates a hard link to `src` inside the jail directory.
274+
func (j *runcJailer) makeLinkInJail(src, base string) (string, error) {
274275
root := j.RootPath()
275276

276-
base := filepath.Base(src)
277+
if strings.ContainsRune(base, os.PathSeparator) {
278+
return "", fmt.Errorf("%q must not contain %q", base, os.PathSeparator)
279+
}
280+
277281
dst := filepath.Join(root, base)
278282

279283
// Since Firecracker is unaware that we are in a jailed environment and
@@ -294,13 +298,13 @@ func (j *runcJailer) BuildLinkFifoHandler() firecracker.Handler {
294298
return firecracker.Handler{
295299
Name: jailerFifoHandlerName,
296300
Fn: func(ctx context.Context, m *firecracker.Machine) error {
297-
logFifo, err := j.makeLinkInJail(m.Cfg.LogPath)
301+
logFifo, err := j.makeLinkInJail(m.Cfg.LogPath, internal.FirecrackerLogFifoName)
298302
if err != nil {
299303
return err
300304
}
301305
m.Cfg.LogFifo = logFifo
302306

303-
metricsFifo, err := j.makeLinkInJail(m.Cfg.MetricsPath)
307+
metricsFifo, err := j.makeLinkInJail(m.Cfg.MetricsPath, internal.FirecrackerMetricsFifoName)
304308
if err != nil {
305309
return err
306310
}

runtime/runc_jailer_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,66 @@ func TestBindMountToJail_Isolated(t *testing.T) {
171171
)
172172
require.Error(t, err)
173173
}
174+
175+
func TestFifoHandler_Isolated(t *testing.T) {
176+
// Because of chown(2).
177+
internal.RequiresIsolation(t)
178+
179+
testcases := []struct {
180+
name string
181+
logPath string
182+
metricsPath string
183+
}{
184+
{
185+
"Different basename",
186+
"log.fifo",
187+
"metrics.fifo",
188+
},
189+
{
190+
"Same basename",
191+
"log/vmid.fifo",
192+
"metrics/vmid.fifo",
193+
},
194+
}
195+
196+
for _, tc := range testcases {
197+
tc := tc
198+
t.Run(tc.name, func(t *testing.T) {
199+
dir, err := ioutil.TempDir("", testNameToVMID(t.Name()))
200+
require.NoError(t, err)
201+
202+
logPath := filepath.Join(dir, tc.logPath)
203+
metricsPath := filepath.Join(dir, tc.metricsPath)
204+
205+
err = os.MkdirAll(filepath.Dir(logPath), 0750)
206+
require.NoError(t, err)
207+
err = ioutil.WriteFile(logPath, []byte("log"), 0644)
208+
require.NoError(t, err)
209+
210+
err = os.MkdirAll(filepath.Dir(metricsPath), 0750)
211+
require.NoError(t, err)
212+
err = ioutil.WriteFile(metricsPath, []byte("metrics"), 0644)
213+
require.NoError(t, err)
214+
215+
j := runcJailer{
216+
Config: runcJailerConfig{
217+
OCIBundlePath: dir,
218+
},
219+
}
220+
err = os.Mkdir(j.RootPath(), 0750)
221+
require.NoError(t, err)
222+
223+
handler := j.BuildLinkFifoHandler()
224+
err = handler.Fn(
225+
context.Background(),
226+
&firecracker.Machine{
227+
Cfg: firecracker.Config{
228+
LogPath: logPath,
229+
MetricsPath: metricsPath,
230+
},
231+
},
232+
)
233+
require.NoError(t, err)
234+
})
235+
}
236+
}

0 commit comments

Comments
 (0)