aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAleksandr Nogikh <nogikh@google.com>2024-03-06 14:47:02 +0100
committerAleksandr Nogikh <nogikh@google.com>2024-03-13 10:31:21 +0000
commit20d042025bd8e1f91bb1fec20ae1509a08bfa4ef (patch)
treee8a3dcb48db65376fdf865480ca5d043f4c7bdcc
parent76713834e4ab721bc6265c89203ee171152cc017 (diff)
sys/linux: clone args before mutation
Not cloning the argument results in replaceArg() replacing a union argument with itself, which may lead to inconsistent resource references. Add an assertion to detect such cases in the future.
-rw-r--r--prog/clone.go4
-rw-r--r--prog/prog.go3
-rw-r--r--sys/linux/init_iptables.go4
-rw-r--r--sys/linux/init_vusb.go4
4 files changed, 11 insertions, 4 deletions
diff --git a/prog/clone.go b/prog/clone.go
index 029cf94c4..4523be0eb 100644
--- a/prog/clone.go
+++ b/prog/clone.go
@@ -39,6 +39,10 @@ func cloneCall(c *Call, newargs map[*ResultArg]*ResultArg) *Call {
return c1
}
+func CloneArg(arg Arg) Arg {
+ return clone(arg, nil)
+}
+
func clone(arg Arg, newargs map[*ResultArg]*ResultArg) Arg {
var arg1 Arg
switch a := arg.(type) {
diff --git a/prog/prog.go b/prog/prog.go
index f93293b64..da6f158bc 100644
--- a/prog/prog.go
+++ b/prog/prog.go
@@ -354,6 +354,9 @@ func (p *Prog) insertBefore(c *Call, calls []*Call) {
// replaceArg replaces arg with arg1 in a program.
func replaceArg(arg, arg1 Arg) {
+ if arg == arg1 {
+ panic("replacing an argument with itself")
+ }
switch a := arg.(type) {
case *ConstArg:
*a = *arg1.(*ConstArg)
diff --git a/sys/linux/init_iptables.go b/sys/linux/init_iptables.go
index 7e96662fb..95825d33c 100644
--- a/sys/linux/init_iptables.go
+++ b/sys/linux/init_iptables.go
@@ -31,7 +31,7 @@ func (arch *arch) generateNetfilterTable(g *prog.Gen, typ prog.Type, dir prog.Di
} else {
// TODO(dvyukov): try to restore original hook order after mutation
// instead of assigning brand new offsets.
- arg = old
+ arg = prog.CloneArg(old)
calls = g.MutateArg(arg)
}
var tableArg *prog.GroupArg
@@ -113,7 +113,7 @@ func (arch *arch) generateEbtables(g *prog.Gen, typ prog.Type, dir prog.Dir, old
} else {
// TODO(dvyukov): try to restore original hook order after mutation
// instead of assigning brand new offsets.
- arg = old
+ arg = prog.CloneArg(old)
calls = g.MutateArg(arg)
}
if g.Target().ArgContainsAny(arg) {
diff --git a/sys/linux/init_vusb.go b/sys/linux/init_vusb.go
index 5e4244f6e..ca645bf45 100644
--- a/sys/linux/init_vusb.go
+++ b/sys/linux/init_vusb.go
@@ -55,7 +55,7 @@ func (arch *arch) generateUsbDeviceDescriptor(g *prog.Gen, typ0 prog.Type, dir p
if old == nil {
arg = g.GenerateSpecialArg(typ0, dir, &calls)
} else {
- arg = old
+ arg = prog.CloneArg(old)
calls = g.MutateArg(arg)
}
if g.Target().ArgContainsAny(arg) {
@@ -144,7 +144,7 @@ func (arch *arch) generateUsbHidDeviceDescriptor(g *prog.Gen, typ0 prog.Type, di
if old == nil {
arg = g.GenerateSpecialArg(typ0, dir, &calls)
} else {
- arg = old
+ arg = prog.CloneArg(old)
calls = g.MutateArg(arg)
}
if g.Target().ArgContainsAny(arg) {