1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
// Copyright 2022 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package seccheck
import (
"fmt"
"os"
"path"
"gvisor.dev/gvisor/pkg/fd"
"gvisor.dev/gvisor/pkg/sync"
)
// PointX represents the checkpoint X.
const (
PointClone Point = iota
PointContainerStart
PointExecve
PointExitNotifyParent
PointTaskExit
// Add new Points above this line.
pointLengthBeforeSyscalls
)
// FieldCtxtX represents a data field that comes from the Context.
const (
FieldCtxtContainerID Field = iota
FieldCtxtCredentials
FieldCtxtCwd
FieldCtxtProcessName
FieldCtxtThreadGroupID
FieldCtxtThreadGroupStartTime
FieldCtxtThreadID
FieldCtxtThreadStartTime
FieldCtxtTime
)
// Fields for container/start point.
const (
// FieldContainerStartEnv is an optional field to collect list of environment
// variables set for the container start process.
FieldContainerStartEnv Field = iota
)
// Fields for sentry/execve point.
const (
// FieldSentryExecveBinaryInfo is an optional field to collect information
// about the binary being executed.
FieldSentryExecveBinaryInfo Field = iota
)
// Points is a map with all the trace points registered in the system.
var Points = map[string]PointDesc{}
// Sinks is a map with all the sinks registered in the system.
var Sinks = map[string]SinkDesc{}
// defaultContextFields are the fields present in most trace points.
var defaultContextFields = []FieldDesc{
{
ID: FieldCtxtTime,
Name: "time",
},
{
ID: FieldCtxtThreadID,
Name: "thread_id",
},
{
ID: FieldCtxtThreadStartTime,
Name: "task_start_time",
},
{
ID: FieldCtxtThreadGroupID,
Name: "group_id",
},
{
ID: FieldCtxtThreadGroupStartTime,
Name: "thread_group_start_time",
},
{
ID: FieldCtxtContainerID,
Name: "container_id",
},
{
ID: FieldCtxtCredentials,
Name: "credentials",
},
{
ID: FieldCtxtCwd,
Name: "cwd",
},
{
ID: FieldCtxtProcessName,
Name: "process_name",
},
}
// SinkDesc describes a sink that is available to be configured.
type SinkDesc struct {
// Name is a unique identifier for the sink.
Name string
// Setup is called outside the protection of the sandbox. This is done to
// allow the sink to do whatever is necessary to set it up. If it returns a
// file, this file is donated to the sandbox and passed to the sink when New
// is called. config is an opaque json object passed to the sink.
Setup func(config map[string]any) (*os.File, error)
// New creates a new sink. config is an opaque json object passed to the sink.
// endpoing is a file descriptor to the file returned in Setup. It's set to -1
// if Setup returned nil.
New func(config map[string]any, endpoint *fd.FD) (Sink, error)
}
// RegisterSink registers a new sink to make it discoverable.
func RegisterSink(sink SinkDesc) {
if _, ok := Sinks[sink.Name]; ok {
panic(fmt.Sprintf("Sink %q already registered", sink.Name))
}
Sinks[sink.Name] = sink
}
// PointDesc describes a Point that is available to be configured.
// Schema for these points are defined in pkg/sentry/seccheck/points/.
type PointDesc struct {
// ID is the point unique identifier.
ID Point
// Name is the point unique name. Convention is to use the following format:
// namespace/name
// Examples: container/start, sentry/clone, etc.
Name string
// OptionalFields is a list of fields that are available in the point, but not
// collected unless specified when the Point is configured.
// Examples: fd_path, data for read/write Points, etc.
OptionalFields []FieldDesc
// ContextFields is a list of fields that can be collected from the context,
// but are not collected unless specified when the Point is configured.
// Examples: container_id, PID, etc.
ContextFields []FieldDesc
}
// FieldDesc describes an optional/context field that is available to be
// configured.
type FieldDesc struct {
// ID is the numeric identifier of the field.
ID Field
// Name is the unique field name.
Name string
}
func registerPoint(pt PointDesc) {
if _, ok := Points[pt.Name]; ok {
panic(fmt.Sprintf("Point %q already registered", pt.Name))
}
if err := validateFields(pt.OptionalFields); err != nil {
panic(err)
}
if err := validateFields(pt.ContextFields); err != nil {
panic(err)
}
Points[pt.Name] = pt
}
func validateFields(fields []FieldDesc) error {
ids := make(map[Field]FieldDesc)
names := make(map[string]FieldDesc)
for _, f := range fields {
if other, ok := names[f.Name]; ok {
return fmt.Errorf("field %q has repeated name with field %q", f.Name, other.Name)
}
if other, ok := ids[f.ID]; ok {
return fmt.Errorf("field %q has repeated ID (%d) with field %q", f.Name, f.ID, other.Name)
}
names[f.Name] = f
ids[f.ID] = f
}
return nil
}
func addRawSyscallPoint(sysno uintptr) {
addSyscallPointHelper(SyscallRawEnter, sysno, fmt.Sprintf("sysno/%d", sysno), nil)
}
func addSyscallPoint(sysno uintptr, name string, optionalFields []FieldDesc) {
addSyscallPointHelper(SyscallEnter, sysno, name, optionalFields)
}
func addSyscallPointHelper(typ SyscallType, sysno uintptr, name string, optionalFields []FieldDesc) {
registerPoint(PointDesc{
ID: GetPointForSyscall(typ, sysno),
Name: path.Join("syscall", name, "enter"),
OptionalFields: optionalFields,
ContextFields: defaultContextFields,
})
registerPoint(PointDesc{
ID: GetPointForSyscall(typ+1, sysno),
Name: path.Join("syscall", name, "exit"),
OptionalFields: optionalFields,
ContextFields: defaultContextFields,
})
}
// genericInit initializes non-architecture-specific Points available in the system.
func genericInit() {
// Points from the container namespace.
registerPoint(PointDesc{
ID: PointContainerStart,
Name: "container/start",
OptionalFields: []FieldDesc{
{
ID: FieldContainerStartEnv,
Name: "env",
},
},
ContextFields: defaultContextFields,
})
// Points from the sentry namespace.
registerPoint(PointDesc{
ID: PointClone,
Name: "sentry/clone",
ContextFields: defaultContextFields,
})
registerPoint(PointDesc{
ID: PointExecve,
Name: "sentry/execve",
OptionalFields: []FieldDesc{
{
ID: FieldSentryExecveBinaryInfo,
Name: "binary_info",
},
},
ContextFields: defaultContextFields,
})
registerPoint(PointDesc{
ID: PointExitNotifyParent,
Name: "sentry/exit_notify_parent",
ContextFields: []FieldDesc{
{
ID: FieldCtxtTime,
Name: "time",
},
{
ID: FieldCtxtThreadID,
Name: "thread_id",
},
{
ID: FieldCtxtThreadStartTime,
Name: "task_start_time",
},
{
ID: FieldCtxtThreadGroupID,
Name: "group_id",
},
{
ID: FieldCtxtThreadGroupStartTime,
Name: "thread_group_start_time",
},
{
ID: FieldCtxtContainerID,
Name: "container_id",
},
{
ID: FieldCtxtCredentials,
Name: "credentials",
},
{
ID: FieldCtxtProcessName,
Name: "process_name",
},
},
})
registerPoint(PointDesc{
ID: PointTaskExit,
Name: "sentry/task_exit",
ContextFields: defaultContextFields,
})
}
var initOnce sync.Once
// Initialize initializes the Points available in the system.
// Must be called prior to using any of them.
func Initialize() {
initOnce.Do(func() {
genericInit()
archInit()
})
}
|