| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"debug/elf" |
| 5 |
"encoding/binary" |
| 6 |
"errors" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"io/fs" |
| 10 |
"math" |
| 11 |
"slices" |
| 12 |
"strings" |
| 13 |
|
| 14 |
"github.com/cilium/ebpf/asm" |
| 15 |
"github.com/cilium/ebpf/btf" |
| 16 |
"github.com/cilium/ebpf/internal" |
| 17 |
"github.com/cilium/ebpf/internal/kallsyms" |
| 18 |
"github.com/cilium/ebpf/internal/platform" |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
type handles []*btf.Handle |
| 23 |
|
| 24 |
func (hs *handles) add(h *btf.Handle) (int, error) { |
| 25 |
if h == nil { |
| 26 |
return 0, nil |
| 27 |
} |
| 28 |
|
| 29 |
if len(*hs) == math.MaxInt16 { |
| 30 |
return 0, fmt.Errorf("can't add more than %d module FDs to fdArray", math.MaxInt16) |
| 31 |
} |
| 32 |
|
| 33 |
*hs = append(*hs, h) |
| 34 |
|
| 35 |
|
| 36 |
return len(*hs), nil |
| 37 |
} |
| 38 |
|
| 39 |
func (hs handles) fdArray() []int32 { |
| 40 |
|
| 41 |
fda := []int32{0} |
| 42 |
for _, h := range hs { |
| 43 |
fda = append(fda, int32(h.FD())) |
| 44 |
} |
| 45 |
|
| 46 |
return fda |
| 47 |
} |
| 48 |
|
| 49 |
func (hs *handles) Close() error { |
| 50 |
var errs []error |
| 51 |
for _, h := range *hs { |
| 52 |
errs = append(errs, h.Close()) |
| 53 |
} |
| 54 |
return errors.Join(errs...) |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func hasFunctionReferences(insns asm.Instructions) bool { |
| 74 |
for _, i := range insns { |
| 75 |
if i.IsFunctionReference() { |
| 76 |
return true |
| 77 |
} |
| 78 |
} |
| 79 |
return false |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func applyRelocations(insns asm.Instructions, bo binary.ByteOrder, b *btf.Builder, c *btf.Cache, kernelOverride *btf.Spec, extraTargets []*btf.Spec) error { |
| 86 |
var relos []*btf.CORERelocation |
| 87 |
var reloInsns []*asm.Instruction |
| 88 |
iter := insns.Iterate() |
| 89 |
for iter.Next() { |
| 90 |
if relo := btf.CORERelocationMetadata(iter.Ins); relo != nil { |
| 91 |
relos = append(relos, relo) |
| 92 |
reloInsns = append(reloInsns, iter.Ins) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if len(relos) == 0 { |
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
if bo == nil { |
| 101 |
bo = internal.NativeEndian |
| 102 |
} |
| 103 |
|
| 104 |
var targets []*btf.Spec |
| 105 |
if kernelOverride == nil { |
| 106 |
kernel, err := c.Kernel() |
| 107 |
if err != nil { |
| 108 |
return fmt.Errorf("load kernel spec: %w", err) |
| 109 |
} |
| 110 |
|
| 111 |
modules, err := c.Modules() |
| 112 |
|
| 113 |
|
| 114 |
if err != nil && !errors.Is(err, fs.ErrNotExist) { |
| 115 |
return err |
| 116 |
} |
| 117 |
|
| 118 |
targets = make([]*btf.Spec, 0, 1+len(modules)+len(extraTargets)) |
| 119 |
targets = append(targets, kernel) |
| 120 |
|
| 121 |
for _, kmod := range modules { |
| 122 |
spec, err := c.Module(kmod) |
| 123 |
if err != nil { |
| 124 |
return fmt.Errorf("load BTF for kmod %s: %w", kmod, err) |
| 125 |
} |
| 126 |
|
| 127 |
targets = append(targets, spec) |
| 128 |
} |
| 129 |
} else { |
| 130 |
|
| 131 |
|
| 132 |
targets = []*btf.Spec{kernelOverride} |
| 133 |
} |
| 134 |
|
| 135 |
targets = append(targets, extraTargets...) |
| 136 |
|
| 137 |
fixups, err := btf.CORERelocate(relos, targets, bo, b.Add) |
| 138 |
if err != nil { |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
for i, fixup := range fixups { |
| 143 |
if err := fixup.Apply(reloInsns[i]); err != nil { |
| 144 |
return fmt.Errorf("fixup for %s: %w", relos[i], err) |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return nil |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
func flattenPrograms(progs map[string]*ProgramSpec, names []string) { |
| 155 |
|
| 156 |
refs := make(map[*ProgramSpec][]string) |
| 157 |
for _, prog := range progs { |
| 158 |
refs[prog] = prog.Instructions.FunctionReferences() |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
flattened := make([]asm.Instructions, 0, len(names)) |
| 164 |
for _, name := range names { |
| 165 |
flattened = append(flattened, flattenInstructions(name, progs, refs)) |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
for i, name := range names { |
| 170 |
progs[name].Instructions = flattened[i] |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
func flattenInstructions(name string, progs map[string]*ProgramSpec, refs map[*ProgramSpec][]string) asm.Instructions { |
| 182 |
prog := progs[name] |
| 183 |
progRefs := refs[prog] |
| 184 |
|
| 185 |
if len(progRefs) == 0 { |
| 186 |
|
| 187 |
return prog.Instructions |
| 188 |
} |
| 189 |
|
| 190 |
insns := make(asm.Instructions, len(prog.Instructions)) |
| 191 |
copy(insns, prog.Instructions) |
| 192 |
|
| 193 |
|
| 194 |
pending := make([]string, len(progRefs)) |
| 195 |
copy(pending, progRefs) |
| 196 |
|
| 197 |
|
| 198 |
linked := make(map[string]bool) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
for len(pending) > 0 { |
| 203 |
var ref string |
| 204 |
ref, pending = pending[0], pending[1:] |
| 205 |
|
| 206 |
if linked[ref] { |
| 207 |
|
| 208 |
continue |
| 209 |
} |
| 210 |
|
| 211 |
progRef := progs[ref] |
| 212 |
if progRef == nil { |
| 213 |
|
| 214 |
|
| 215 |
continue |
| 216 |
} |
| 217 |
|
| 218 |
insns = append(insns, progRef.Instructions...) |
| 219 |
linked[ref] = true |
| 220 |
|
| 221 |
|
| 222 |
pending = append(pending, refs[progRef]...) |
| 223 |
} |
| 224 |
|
| 225 |
return insns |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func fixupAndValidate(insns asm.Instructions) error { |
| 232 |
iter := insns.Iterate() |
| 233 |
for iter.Next() { |
| 234 |
ins := iter.Ins |
| 235 |
|
| 236 |
|
| 237 |
needsMap := ins.Reference() != "" || ins.Metadata.Get(kconfigMetaKey{}) != nil |
| 238 |
if ins.IsLoadFromMap() && needsMap && ins.Map() == nil { |
| 239 |
return fmt.Errorf("instruction %d: %w", iter.Index, asm.ErrUnsatisfiedMapReference) |
| 240 |
} |
| 241 |
|
| 242 |
fixupProbeReadKernel(ins) |
| 243 |
} |
| 244 |
|
| 245 |
return nil |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
const kfuncCallPoisonBase = 0xdedc0de |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
func fixupKfuncs(insns asm.Instructions, cache *btf.Cache) (_ handles, err error) { |
| 258 |
closeOnError := func(c io.Closer) { |
| 259 |
if err != nil { |
| 260 |
c.Close() |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
iter := insns.Iterate() |
| 265 |
for iter.Next() { |
| 266 |
ins := iter.Ins |
| 267 |
if metadata := ins.Metadata.Get(kfuncMetaKey{}); metadata != nil { |
| 268 |
goto fixups |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return nil, nil |
| 273 |
|
| 274 |
fixups: |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
_, err = cache.Kernel() |
| 279 |
|
| 280 |
if errors.Is(err, internal.ErrNotSupportedOnOS) { |
| 281 |
return nil, fmt.Errorf("kfuncs are not supported on this platform: %w", err) |
| 282 |
} |
| 283 |
if err != nil && !errors.Is(err, ErrNotSupported) { |
| 284 |
return nil, err |
| 285 |
} |
| 286 |
|
| 287 |
fdArray := make(handles, 0) |
| 288 |
defer closeOnError(&fdArray) |
| 289 |
|
| 290 |
for { |
| 291 |
ins := iter.Ins |
| 292 |
|
| 293 |
metadata := ins.Metadata.Get(kfuncMetaKey{}) |
| 294 |
if metadata == nil { |
| 295 |
if !iter.Next() { |
| 296 |
|
| 297 |
break |
| 298 |
} |
| 299 |
continue |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
kfm, _ := metadata.(*kfuncMeta) |
| 304 |
if kfm == nil { |
| 305 |
return nil, fmt.Errorf("kfuncMetaKey doesn't contain kfuncMeta") |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
target := btf.Type((*btf.Func)(nil)) |
| 311 |
spec, module, err := findTargetInKernel(kfm.Func.Name, &target, cache) |
| 312 |
if errors.Is(err, btf.ErrNotFound) { |
| 313 |
if kfm.Binding == elf.STB_WEAK { |
| 314 |
if ins.IsKfuncCall() { |
| 315 |
|
| 316 |
|
| 317 |
fn, err := asm.BuiltinFuncForPlatform(platform.Native, kfuncCallPoisonBase) |
| 318 |
if err != nil { |
| 319 |
return nil, err |
| 320 |
} |
| 321 |
*ins = fn.Call() |
| 322 |
} else if ins.OpCode.IsDWordLoad() { |
| 323 |
|
| 324 |
ins.Constant = 0 |
| 325 |
ins.Src = 0 |
| 326 |
} else { |
| 327 |
return nil, fmt.Errorf("only kfunc calls and dword loads may have kfunc metadata") |
| 328 |
} |
| 329 |
|
| 330 |
iter.Next() |
| 331 |
continue |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
return nil, fmt.Errorf("kfunc %q: %w", kfm.Func.Name, ErrNotSupported) |
| 336 |
} |
| 337 |
if err != nil { |
| 338 |
return nil, fmt.Errorf("finding kfunc in kernel: %w", err) |
| 339 |
} |
| 340 |
|
| 341 |
idx, err := fdArray.add(module) |
| 342 |
if err != nil { |
| 343 |
return nil, err |
| 344 |
} |
| 345 |
|
| 346 |
if err := btf.CheckTypeCompatibility(kfm.Func.Type, target.(*btf.Func).Type); err != nil { |
| 347 |
return nil, &incompatibleKfuncError{kfm.Func.Name, err} |
| 348 |
} |
| 349 |
|
| 350 |
id, err := spec.TypeID(target) |
| 351 |
if err != nil { |
| 352 |
return nil, err |
| 353 |
} |
| 354 |
|
| 355 |
ins.Constant = int64(id) |
| 356 |
ins.Offset = int16(idx) |
| 357 |
|
| 358 |
if !iter.Next() { |
| 359 |
break |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
return fdArray, nil |
| 364 |
} |
| 365 |
|
| 366 |
type incompatibleKfuncError struct { |
| 367 |
name string |
| 368 |
err error |
| 369 |
} |
| 370 |
|
| 371 |
func (ike *incompatibleKfuncError) Error() string { |
| 372 |
return fmt.Sprintf("kfunc %q: %s", ike.name, ike.err) |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
func fixupProbeReadKernel(ins *asm.Instruction) { |
| 378 |
if !ins.IsBuiltinCall() { |
| 379 |
return |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
if haveProbeReadKernel() == nil { |
| 384 |
return |
| 385 |
} |
| 386 |
|
| 387 |
switch asm.BuiltinFunc(ins.Constant) { |
| 388 |
case asm.FnProbeReadKernel, asm.FnProbeReadUser: |
| 389 |
ins.Constant = int64(asm.FnProbeRead) |
| 390 |
case asm.FnProbeReadKernelStr, asm.FnProbeReadUserStr: |
| 391 |
ins.Constant = int64(asm.FnProbeReadStr) |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
func resolveKconfigReferences(insns asm.Instructions) (_ *Map, err error) { |
| 399 |
closeOnError := func(c io.Closer) { |
| 400 |
if err != nil { |
| 401 |
c.Close() |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
var spec *MapSpec |
| 406 |
iter := insns.Iterate() |
| 407 |
for iter.Next() { |
| 408 |
meta, _ := iter.Ins.Metadata.Get(kconfigMetaKey{}).(*kconfigMeta) |
| 409 |
if meta != nil { |
| 410 |
spec = meta.Map |
| 411 |
break |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
if spec == nil { |
| 416 |
return nil, nil |
| 417 |
} |
| 418 |
|
| 419 |
cpy := spec.Copy() |
| 420 |
if err := resolveKconfig(cpy); err != nil { |
| 421 |
return nil, err |
| 422 |
} |
| 423 |
|
| 424 |
kconfig, err := NewMap(cpy) |
| 425 |
if err != nil { |
| 426 |
return nil, err |
| 427 |
} |
| 428 |
defer closeOnError(kconfig) |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
iter = insns.Iterate() |
| 433 |
for iter.Next() { |
| 434 |
meta, _ := iter.Ins.Metadata.Get(kconfigMetaKey{}).(*kconfigMeta) |
| 435 |
if meta == nil { |
| 436 |
continue |
| 437 |
} |
| 438 |
|
| 439 |
if meta.Map != spec { |
| 440 |
return nil, fmt.Errorf("instruction %d: reference to multiple .kconfig maps is not allowed", iter.Index) |
| 441 |
} |
| 442 |
|
| 443 |
if err := iter.Ins.AssociateMap(kconfig); err != nil { |
| 444 |
return nil, fmt.Errorf("instruction %d: %w", iter.Index, err) |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
iter.Ins.Constant = int64(uint64(meta.Offset) << 32) |
| 449 |
iter.Ins.Metadata.Set(kconfigMetaKey{}, nil) |
| 450 |
} |
| 451 |
|
| 452 |
return kconfig, nil |
| 453 |
} |
| 454 |
|
| 455 |
func resolveKsymReferences(insns asm.Instructions) error { |
| 456 |
type fixup struct { |
| 457 |
*asm.Instruction |
| 458 |
*ksymMeta |
| 459 |
} |
| 460 |
|
| 461 |
var symbols map[string]uint64 |
| 462 |
var fixups []fixup |
| 463 |
|
| 464 |
iter := insns.Iterate() |
| 465 |
for iter.Next() { |
| 466 |
ins := iter.Ins |
| 467 |
meta, _ := ins.Metadata.Get(ksymMetaKey{}).(*ksymMeta) |
| 468 |
if meta == nil { |
| 469 |
continue |
| 470 |
} |
| 471 |
|
| 472 |
if symbols == nil { |
| 473 |
symbols = make(map[string]uint64) |
| 474 |
} |
| 475 |
|
| 476 |
symbols[meta.Name] = 0 |
| 477 |
fixups = append(fixups, fixup{ |
| 478 |
iter.Ins, meta, |
| 479 |
}) |
| 480 |
} |
| 481 |
|
| 482 |
if len(symbols) == 0 { |
| 483 |
return nil |
| 484 |
} |
| 485 |
|
| 486 |
err := kallsyms.AssignAddresses(symbols) |
| 487 |
|
| 488 |
|
| 489 |
if err != nil && !errors.Is(err, ErrRestrictedKernel) { |
| 490 |
return fmt.Errorf("resolve ksyms: %w", err) |
| 491 |
} |
| 492 |
|
| 493 |
var missing []string |
| 494 |
for _, fixup := range fixups { |
| 495 |
addr := symbols[fixup.Name] |
| 496 |
|
| 497 |
if addr == 0 && fixup.Binding != elf.STB_WEAK { |
| 498 |
if !slices.Contains(missing, fixup.Name) { |
| 499 |
missing = append(missing, fixup.Name) |
| 500 |
} |
| 501 |
continue |
| 502 |
} |
| 503 |
|
| 504 |
fixup.Constant = int64(addr) |
| 505 |
} |
| 506 |
|
| 507 |
if len(missing) > 0 { |
| 508 |
if err != nil { |
| 509 |
|
| 510 |
return fmt.Errorf("resolve required ksyms: %s: %w", strings.Join(missing, ","), err) |
| 511 |
} |
| 512 |
|
| 513 |
return fmt.Errorf("kernel is missing symbol: %s", strings.Join(missing, ",")) |
| 514 |
} |
| 515 |
|
| 516 |
return nil |
| 517 |
} |