Skip to content

Commit fcf9dc2

Browse files
committed
Use lastest OCI runtime-spec
The runtime-spec changes were already merged in this PR: opencontainers/runtime-spec#1074 To reference the new merge commit with the latest fixes I just run: go get 'github.com/opencontainers/runtime-spec@9c848d91e8cf872e7453296832d66de6325e1e25 go mod vendor And commited the changes. I also update the code to match the new spec. While doing that, I rewrote the code to close fds in _most_ cases. It is tricky to close them before we have a reference to the `fds` slice, so that is left as a follow-up improvement. Signed-off-by: Rodrigo Campos <[email protected]>
1 parent fff5b2d commit fcf9dc2

File tree

6 files changed

+79
-35
lines changed

6 files changed

+79
-35
lines changed

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.15
55
require (
66
github.com/gogo/protobuf v1.3.2 // indirect
77
github.com/google/go-cmp v0.5.4 // indirect
8-
github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d
8+
github.com/opencontainers/runtime-spec v1.0.3-0.20210319114652-9c848d91e8cf
99
github.com/seccomp/libseccomp-golang v0.9.1
1010
github.com/sirupsen/logrus v1.7.0
1111
golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect
@@ -17,6 +17,4 @@ require (
1717
k8s.io/klog/v2 v2.6.0 // indirect
1818
)
1919

20-
replace github.com/opencontainers/runtime-spec => github.com/kinvolk/runtime-spec v1.0.2-0.20201110202115-2755fc508653
21-
2220
replace github.com/seccomp/libseccomp-golang => github.com/kinvolk/libseccomp-golang v0.9.2-0.20201113182948-883917843313

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
143143
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
144144
github.com/kinvolk/libseccomp-golang v0.9.2-0.20201113182948-883917843313 h1:Yksjjb26OqF2mRph6uc7HUJA3p/UqbehaxfV9g1wx2k=
145145
github.com/kinvolk/libseccomp-golang v0.9.2-0.20201113182948-883917843313/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
146-
github.com/kinvolk/runtime-spec v1.0.2-0.20201110202115-2755fc508653 h1:amSDd4i3F4aNzi9TmDRDjW31ZQ6T9LN1lXVOazoTjQ8=
147-
github.com/kinvolk/runtime-spec v1.0.2-0.20201110202115-2755fc508653/go.mod h1:x0jDMgm6GEAbohE2lugQZrRwSe07FpeoUJm9jP2a5Sk=
148146
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
149147
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
150148
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@@ -172,6 +170,8 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W
172170
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
173171
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
174172
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
173+
github.com/opencontainers/runtime-spec v1.0.3-0.20210319114652-9c848d91e8cf h1:mLvxOVCb87NHTjM+IYwG2xtYtdaC8zjix+yqDx0Av/Q=
174+
github.com/opencontainers/runtime-spec v1.0.3-0.20210319114652-9c848d91e8cf/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0=
175175
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
176176
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
177177
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

pkg/agent/agent.go

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,56 @@ import (
3030
"github.com/kinvolk/seccompagent/pkg/registry"
3131
)
3232

33+
func closeStateFds(recvFds []int) {
34+
// If performance becomes an issue, we can fallback to the new syscall closerange().
35+
for i := range recvFds {
36+
// Ignore the return code. There isn't anything better to do.
37+
unix.Close(i)
38+
}
39+
}
40+
41+
// parseContainerProcessState returns the seccomp-fd and closes the rest of the fds in recvFds.
42+
// In case of error, no fd is closed.
43+
// StateFds is assumed to be formated as specs.ContainerProcessState.Fds and
44+
// recvFds the corresponding list of received fds in the same SCM_RIGHT message.
45+
func parseStateFds(stateFds []string, recvFds []int) (uintptr, error) {
46+
// Lets find the index in stateFds of the seccomp-fd.
47+
idx := -1
48+
err := false
49+
50+
for i, name := range stateFds {
51+
if name == specs.SeccompFdName && idx == -1 {
52+
idx = i
53+
continue
54+
}
55+
56+
// We found the seccompFdName two times. Error out!
57+
if name == specs.SeccompFdName && idx != -1 {
58+
err = true
59+
}
60+
}
61+
62+
if idx == -1 || err {
63+
return 0, fmt.Errorf("seccomp fd not found or malformed containerProcessState.Fds")
64+
}
65+
66+
if idx >= len(recvFds) || idx < 0 {
67+
return 0, fmt.Errorf("seccomp fd index out of range")
68+
}
69+
70+
fd := uintptr(recvFds[idx])
71+
72+
for i := range recvFds {
73+
if i == idx {
74+
continue
75+
}
76+
77+
unix.Close(recvFds[i])
78+
}
79+
80+
return fd, nil
81+
}
82+
3383
func receiveNewSeccompFile(resolver registry.ResolverFunc, sockfd int) (*registry.Registry, *os.File, error) {
3484
MaxNameLen := 4096
3585

@@ -56,16 +106,6 @@ func receiveNewSeccompFile(resolver registry.ResolverFunc, sockfd int) (*registr
56106
stateBuf = stateBuf[:n]
57107
oob = oob[:oobn]
58108

59-
containerProcessState := &specs.ContainerProcessState{}
60-
err = json.Unmarshal(stateBuf, containerProcessState)
61-
if err != nil {
62-
return nil, nil, fmt.Errorf("cannot parse OCI state: %v\n", err)
63-
}
64-
seccompFdIndex, ok := containerProcessState.FdIndexes["seccompFd"]
65-
if !ok || seccompFdIndex < 0 {
66-
return nil, nil, fmt.Errorf("recvfd: didn't receive seccomp fd")
67-
}
68-
69109
scms, err := unix.ParseSocketControlMessage(oob)
70110
if err != nil {
71111
return nil, nil, err
@@ -75,14 +115,28 @@ func receiveNewSeccompFile(resolver registry.ResolverFunc, sockfd int) (*registr
75115
}
76116
scm := scms[0]
77117

118+
// The fds are added just after executing recvmsg(). So, since then
119+
// until here, if we return, we are leaking fds.
120+
// However, it is tricky to close the fds before we have a reference to
121+
// the fds slice, that we create just here.
122+
// TODO: Close fds if we return before this too.
78123
fds, err := unix.ParseUnixRights(&scm)
79124
if err != nil {
80125
return nil, nil, err
81126
}
82-
if seccompFdIndex >= len(fds) {
83-
return nil, nil, fmt.Errorf("recvfd: number of fds is %d and seccompFdIndex is %d", len(fds), seccompFdIndex)
127+
128+
containerProcessState := &specs.ContainerProcessState{}
129+
err = json.Unmarshal(stateBuf, containerProcessState)
130+
if err != nil {
131+
closeStateFds(fds)
132+
return nil, nil, fmt.Errorf("cannot parse OCI state: %v\n", err)
133+
}
134+
135+
fd, err := parseStateFds(containerProcessState.Fds, fds)
136+
if err != nil {
137+
closeStateFds(fds)
138+
return nil, nil, err
84139
}
85-
fd := uintptr(fds[seccompFdIndex])
86140

87141
log.WithFields(log.Fields{
88142
"fd": fd,
@@ -92,12 +146,6 @@ func receiveNewSeccompFile(resolver registry.ResolverFunc, sockfd int) (*registr
92146
"annotations": containerProcessState.State.Annotations,
93147
}).Debug("New seccomp fd received on socket")
94148

95-
for i := 0; i < len(fds); i++ {
96-
if i != seccompFdIndex {
97-
unix.Close(fds[i])
98-
}
99-
}
100-
101149
var reg *registry.Registry
102150
if resolver != nil {
103151
reg = resolver(containerProcessState)

vendor/github.com/opencontainers/runtime-spec/specs-go/config.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/opencontainers/runtime-spec/specs-go/state.go

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ github.com/json-iterator/go
2626
github.com/modern-go/concurrent
2727
# github.com/modern-go/reflect2 v1.0.1
2828
github.com/modern-go/reflect2
29-
# github.com/opencontainers/runtime-spec v1.0.3-0.20200929063507-e6143ca7d51d => github.com/kinvolk/runtime-spec v1.0.2-0.20201110202115-2755fc508653
29+
# github.com/opencontainers/runtime-spec v1.0.3-0.20210319114652-9c848d91e8cf
3030
## explicit
3131
github.com/opencontainers/runtime-spec/specs-go
3232
# github.com/seccomp/libseccomp-golang v0.9.1 => github.com/kinvolk/libseccomp-golang v0.9.2-0.20201113182948-883917843313
@@ -258,5 +258,4 @@ k8s.io/utils/integer
258258
sigs.k8s.io/structured-merge-diff/v4/value
259259
# sigs.k8s.io/yaml v1.2.0
260260
sigs.k8s.io/yaml
261-
# github.com/opencontainers/runtime-spec => github.com/kinvolk/runtime-spec v1.0.2-0.20201110202115-2755fc508653
262261
# github.com/seccomp/libseccomp-golang => github.com/kinvolk/libseccomp-golang v0.9.2-0.20201113182948-883917843313

0 commit comments

Comments
 (0)