Skip to content

Commit f6a5ebd

Browse files
lmbti-mo
authored andcommitted
map, program: always sanitize names passed to the kernel
The kernel only allows a restricted set of characters when naming a map or program. So far we've required the user to call SanitizeName manually to ensure that the Name is valid. Always sanitize names and unexport SanitizeName. The ELF reader still calls sanitizeName to avoid changing the output too much. Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
1 parent 7b8cc32 commit f6a5ebd

File tree

7 files changed

+35
-24
lines changed

7 files changed

+35
-24
lines changed

elf_reader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ func (ec *elfCode) loadMaps() error {
742742
lr := io.LimitReader(r, int64(size))
743743

744744
spec := MapSpec{
745-
Name: SanitizeName(mapName, -1),
745+
Name: sanitizeName(mapName, -1),
746746
}
747747
switch {
748748
case binary.Read(lr, ec.ByteOrder, &spec.Type) != nil:
@@ -1029,7 +1029,7 @@ func mapSpecFromBTF(es *elfSection, vs *btf.VarSecinfo, def *btf.Struct, spec *b
10291029
}
10301030

10311031
return &MapSpec{
1032-
Name: SanitizeName(name, -1),
1032+
Name: sanitizeName(name, -1),
10331033
Type: MapType(mapType),
10341034
KeySize: keySize,
10351035
ValueSize: valueSize,
@@ -1156,7 +1156,7 @@ func (ec *elfCode) loadDataSections() error {
11561156
}
11571157

11581158
mapSpec := &MapSpec{
1159-
Name: SanitizeName(sec.Name, -1),
1159+
Name: sanitizeName(sec.Name, -1),
11601160
Type: Array,
11611161
KeySize: 4,
11621162
ValueSize: uint32(sec.Size),

map.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ type MapID = sys.MapID
5050

5151
// MapSpec defines a Map.
5252
type MapSpec struct {
53-
// Name is passed to the kernel as a debug aid. Must only contain
54-
// alpha numeric and '_' characters.
53+
// Name is passed to the kernel as a debug aid.
54+
//
55+
// Unsupported characters will be stripped.
5556
Name string
5657
Type MapType
5758
KeySize uint32

map_test.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,20 +1362,23 @@ func TestMapMarshalUnsafe(t *testing.T) {
13621362
}
13631363

13641364
func TestMapName(t *testing.T) {
1365-
if err := haveObjName(); err != nil {
1366-
t.Skip(err)
1367-
}
1365+
testutils.SkipIfNotSupported(t, haveObjName())
13681366

1369-
m := createMap(t, Array, 1)
1367+
m := mustNewMap(t, &MapSpec{
1368+
Name: "test!123",
1369+
Type: Array,
1370+
KeySize: 4,
1371+
ValueSize: 4,
1372+
MaxEntries: 1,
1373+
}, nil)
13701374

13711375
var info sys.MapInfo
13721376
if err := sys.ObjInfo(m.fd, &info); err != nil {
13731377
t.Fatal(err)
13741378
}
13751379

1376-
if name := unix.ByteSliceToString(info.Name[:]); name != "test" {
1377-
t.Error("Expected name to be test, got", name)
1378-
}
1380+
name := unix.ByteSliceToString(info.Name[:])
1381+
qt.Assert(t, qt.Equals(name, "test123"))
13791382
}
13801383

13811384
func TestMapFromFD(t *testing.T) {

prog.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ type ProgramOptions struct {
106106

107107
// ProgramSpec defines a Program.
108108
type ProgramSpec struct {
109-
// Name is passed to the kernel as a debug aid. Must only contain
110-
// alpha numeric and '_' characters.
109+
// Name is passed to the kernel as a debug aid.
110+
//
111+
// Unsupported characters will be stripped.
111112
Name string
112113

113114
// Type determines at which hook in the kernel a program will run.

prog_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -479,20 +479,25 @@ func TestProgramWithUnsatisfiedMap(t *testing.T) {
479479
}
480480

481481
func TestProgramName(t *testing.T) {
482-
if err := haveObjName(); err != nil {
483-
t.Skip(err)
484-
}
482+
testutils.SkipIfNotSupported(t, haveObjName())
485483

486-
prog := createBasicProgram(t)
484+
prog := mustNewProgram(t, &ProgramSpec{
485+
Name: "test*123",
486+
Type: SocketFilter,
487+
Instructions: asm.Instructions{
488+
asm.LoadImm(asm.R0, 1, asm.DWord),
489+
asm.Return(),
490+
},
491+
License: "MIT",
492+
}, nil)
487493

488494
var info sys.ProgInfo
489495
if err := sys.ObjInfo(prog.fd, &info); err != nil {
490496
t.Fatal(err)
491497
}
492498

493-
if name := unix.ByteSliceToString(info.Name[:]); name != "test" {
494-
t.Errorf("Name is not test, got '%s'", name)
495-
}
499+
name := unix.ByteSliceToString(info.Name[:])
500+
qt.Assert(t, qt.Equals(name, "test123"))
496501
}
497502

498503
func TestProgramCloneNil(t *testing.T) {

syscalls.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ var (
2727
sysErrNotSupported = sys.Error(ErrNotSupported, sys.ENOTSUPP)
2828
)
2929

30-
// SanitizeName replaces all invalid characters in name with replacement.
30+
// sanitizeName replaces all invalid characters in name with replacement.
3131
// Passing a negative value for replacement will delete characters instead
3232
// of replacing them.
3333
//
3434
// The set of allowed characters may change over time.
35-
func SanitizeName(name string, replacement rune) string {
35+
func sanitizeName(name string, replacement rune) string {
3636
return strings.Map(func(char rune) rune {
3737
switch {
3838
case char >= 'A' && char <= 'Z':
@@ -56,6 +56,7 @@ func maybeFillObjName(name string) sys.ObjName {
5656
return sys.ObjName{}
5757
}
5858

59+
name = sanitizeName(name, -1)
5960
if errors.Is(objNameAllowsDot(), ErrNotSupported) {
6061
name = strings.ReplaceAll(name, ".", "")
6162
}

syscalls_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestSanitizeName(t *testing.T) {
1919
"t_est": "t_est",
2020
"hörnchen": "hrnchen",
2121
} {
22-
qt.Assert(t, qt.Equals(SanitizeName(input, -1), want), qt.Commentf("input: %s", input))
22+
qt.Assert(t, qt.Equals(sanitizeName(input, -1), want), qt.Commentf("input: %s", input))
2323
}
2424
}
2525

0 commit comments

Comments
 (0)