Skip to content

Commit 1896d9b

Browse files
committed
fix: add i,j params size limitation
1 parent 1abd24b commit 1896d9b

File tree

6 files changed

+21
-11
lines changed

6 files changed

+21
-11
lines changed

device/awg/special_handshake_handler.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ type SpecialHandshakeHandler struct {
3131
IsSet bool
3232
}
3333

34-
func (handler *SpecialHandshakeHandler) Validate() error {
34+
func (handler *SpecialHandshakeHandler) Validate(maxSegmentSize int) error {
3535
var errs []error
36-
if err := handler.SpecialJunk.Validate(); err != nil {
36+
if err := handler.SpecialJunk.Validate(maxSegmentSize); err != nil {
3737
errs = append(errs, err)
3838
}
39-
if err := handler.ControlledJunk.Validate(); err != nil {
39+
if err := handler.ControlledJunk.Validate(maxSegmentSize); err != nil {
4040
errs = append(errs, err)
4141
}
4242
return errors.Join(errs...)

device/awg/tag_junk_packet_generator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ func (tg *TagJunkPacketGenerator) IpcGetFields() IpcFields {
5757
Value: tg.tagValue,
5858
}
5959
}
60+
61+
func (tg *TagJunkPacketGenerator) Size() int {
62+
return tg.packetSize
63+
}

device/awg/tag_junk_packet_generators.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package awg
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
)
46

57
type TagJunkPacketGenerators struct {
68
tagGenerators []TagJunkPacketGenerator
@@ -20,7 +22,7 @@ func (generators *TagJunkPacketGenerators) IsDefined() bool {
2022
}
2123

2224
// validate that packets were defined consecutively
23-
func (generators *TagJunkPacketGenerators) Validate() error {
25+
func (generators *TagJunkPacketGenerators) Validate(maxSegmentSize int) error {
2426
seen := make([]bool, len(generators.tagGenerators))
2527
for _, generator := range generators.tagGenerators {
2628
index, err := generator.nameIndex()
@@ -32,6 +34,10 @@ func (generators *TagJunkPacketGenerators) Validate() error {
3234
} else {
3335
seen[index-1] = true
3436
}
37+
38+
if generator.Size() > maxSegmentSize {
39+
return fmt.Errorf("junk packet %s must not exceed %d bytes", generator.name, maxSegmentSize)
40+
}
3541
}
3642

3743
for _, found := range seen {

device/awg/tag_junk_packet_generators_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestTagJunkGeneratorHandlerValidate(t *testing.T) {
9191
generators.AppendGenerator(gen)
9292
}
9393

94-
err := generators.Validate()
94+
err := generators.Validate(1500)
9595
if tt.wantErr {
9696
require.Error(t, err)
9797
require.Contains(t, err.Error(), tt.errMsg)

device/device.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ func (device *Device) handlePostConfig(tempAwg *awg.Protocol) error {
819819
}
820820

821821
if tempAwg.HandshakeHandler.IsSet {
822-
if err := tempAwg.HandshakeHandler.Validate(); err != nil {
822+
if err := tempAwg.HandshakeHandler.Validate(MaxSegmentSize); err != nil {
823823
errs = append(errs, ipcErrorf(
824824
ipc.IpcErrorInvalid, "handshake handler validate: %w", err))
825825
} else {

device/uapi.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,26 +406,26 @@ func (device *Device) handleDeviceLine(key, value string, tempAwg *awg.Protocol)
406406
return nil
407407
}
408408

409-
generators, err := awg.Parse(key, value)
409+
generator, err := awg.Parse(key, value)
410410
if err != nil {
411411
return ipcErrorf(ipc.IpcErrorInvalid, "invalid %s: %w", key, err)
412412
}
413413
device.log.Verbosef("UAPI: Updating %s", key)
414-
tempAwg.HandshakeHandler.SpecialJunk.AppendGenerator(generators)
414+
tempAwg.HandshakeHandler.SpecialJunk.AppendGenerator(generator)
415415
tempAwg.HandshakeHandler.IsSet = true
416416
case "j1", "j2", "j3":
417417
if len(value) == 0 {
418418
device.log.Verbosef("UAPI: received empty %s", key)
419419
return nil
420420
}
421421

422-
generators, err := awg.Parse(key, value)
422+
generator, err := awg.Parse(key, value)
423423
if err != nil {
424424
return ipcErrorf(ipc.IpcErrorInvalid, "invalid %s: %w", key, err)
425425
}
426426
device.log.Verbosef("UAPI: Updating %s", key)
427427

428-
tempAwg.HandshakeHandler.ControlledJunk.AppendGenerator(generators)
428+
tempAwg.HandshakeHandler.ControlledJunk.AppendGenerator(generator)
429429
tempAwg.HandshakeHandler.IsSet = true
430430
case "itime":
431431
if len(value) == 0 {

0 commit comments

Comments
 (0)