github.com/google/syzkaller@v0.0.0-20251211124644-a066d2bc4b02/sys/linux/init_test.go (about)

     1  // Copyright 2018 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package linux_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/google/syzkaller/prog"
    10  	_ "github.com/google/syzkaller/sys"
    11  	"github.com/google/syzkaller/sys/targets"
    12  )
    13  
    14  func TestNeutralize(t *testing.T) {
    15  	prog.TestDeserializeHelper(t, targets.Linux, targets.AMD64, nil, []prog.DeserializeTest{
    16  		{
    17  			In:  `syslog(0x10000000006, 0x0, 0x0)`,
    18  			Out: `syslog(0x9, 0x0, 0x0)`,
    19  		},
    20  		{
    21  			In:  `syslog(0x10000000007, 0x0, 0x0)`,
    22  			Out: `syslog(0x9, 0x0, 0x0)`,
    23  		},
    24  		{
    25  			In: `syslog(0x1, 0x0, 0x0)`,
    26  		},
    27  
    28  		{
    29  			In:  `ptrace(0xf000000000, 0x0)`,
    30  			Out: `ptrace(0xffffffffffffffff, 0x0)`,
    31  		},
    32  		{
    33  			In:  `ptrace$peek(0x0, 0x0, &(0x7f0000000000))`,
    34  			Out: `ptrace$peek(0xffffffffffffffff, 0x0, &(0x7f0000000000))`,
    35  		},
    36  		{
    37  			In: `ptrace(0x1, 0x0)`,
    38  		},
    39  		{
    40  			In:  `arch_prctl$ARCH_SET_GS(0xf00000001002, 0x0)`,
    41  			Out: `arch_prctl$ARCH_SET_GS(0x1001, 0x0)`,
    42  		},
    43  		{
    44  			In: `arch_prctl$ARCH_SET_GS(0x1003, 0x0)`,
    45  		},
    46  		{
    47  			In:  `ioctl(0x0, 0x200000c0045877, 0x0)`,
    48  			Out: `ioctl(0x0, 0xc0045878, 0x0)`,
    49  		},
    50  		{
    51  			In:  `fanotify_mark(0x1, 0x2, 0x407fe029, 0x3, 0x0)`,
    52  			Out: `fanotify_mark(0x1, 0x2, 0x4078e029, 0x3, 0x0)`,
    53  		},
    54  		{
    55  			In: `fanotify_mark(0xffffffffffffffff, 0xffffffffffffffff, 0xfffffffffff8ffff, 0xffffffffffffffff, 0x0)`,
    56  		},
    57  		{
    58  			In:  `syz_init_net_socket$bt_hci(0x1, 0x0, 0x0)`,
    59  			Out: `syz_init_net_socket$bt_hci(0xffffffffffffffff, 0x0, 0x0)`,
    60  		},
    61  		{
    62  			In: `syz_init_net_socket$bt_hci(0x27, 0x0, 0x0)`,
    63  		},
    64  		{
    65  			In: `syz_init_net_socket$bt_hci(0x1a, 0x0, 0x0)`,
    66  		},
    67  		{
    68  			In: `syz_init_net_socket$bt_hci(0x1f, 0x0, 0x0)`,
    69  		},
    70  		{
    71  			In:  `mmap(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)`,
    72  			Out: `mmap(0x0, 0x0, 0x0, 0x10, 0x0, 0x0)`,
    73  		},
    74  		{
    75  			In: `mremap(0x0, 0x0, 0x0, 0xcc, 0x0)`,
    76  		},
    77  		{
    78  			In:  `mremap(0x0, 0x0, 0x0, 0xcd, 0x0)`,
    79  			Out: `mremap(0x0, 0x0, 0x0, 0xcf, 0x0)`,
    80  		},
    81  		{
    82  			In: `
    83  mknod(0x0, 0x1000, 0x0)
    84  mknod(0x0, 0x8000, 0x0)
    85  mknod(0x0, 0xc000, 0x0)
    86  mknod(0x0, 0x2000, 0x0)
    87  mknod(0x0, 0x6000, 0x0)
    88  mknod(0x0, 0x6000, 0x700)
    89  `,
    90  			Out: `
    91  mknod(0x0, 0x1000, 0x0)
    92  mknod(0x0, 0x8000, 0x0)
    93  mknod(0x0, 0xc000, 0x0)
    94  mknod(0x0, 0x8000, 0x0)
    95  mknod(0x0, 0x8000, 0x0)
    96  mknod(0x0, 0x6000, 0x700)
    97  `,
    98  		},
    99  		{
   100  			In: `
   101  exit(0x3)
   102  exit(0x43)
   103  exit(0xc3)
   104  exit(0xc3)
   105  exit_group(0x5a)
   106  exit_group(0x43)
   107  exit_group(0x443)
   108  `,
   109  			Out: `
   110  exit(0x3)
   111  exit(0x1)
   112  exit(0x1)
   113  exit(0x1)
   114  exit_group(0x5a)
   115  exit_group(0x1)
   116  exit_group(0x1)
   117  `,
   118  		},
   119  		{
   120  			In: `
   121  syz_open_dev$tty1(0xc, 0x4, 0x4)
   122  syz_open_dev$tty1(0xb, 0x2, 0x4)
   123  syz_open_dev$tty1(0xc, 0x4, 0x5)
   124  `,
   125  			Out: `
   126  syz_open_dev$tty1(0xc, 0x4, 0x4)
   127  syz_open_dev$tty1(0xc, 0x4, 0x4)
   128  syz_open_dev$tty1(0xc, 0x4, 0x1)
   129  `,
   130  		},
   131  		{
   132  			In:  `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x1, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, 0x0)`,
   133  			Out: `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x0, 0x0, 0x0, 0x3}, 0x0)`,
   134  		},
   135  		{
   136  			In:  `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x2, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, 0x0)`,
   137  			Out: `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x0, 0x0, 0x0, 0x3}, 0x0)`,
   138  		},
   139  		{
   140  			In:  `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x3, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0, 0x0, 0x0}, 0x0)`,
   141  			Out: `sched_setattr(0x0, &(0x7f00000002c0)={0x0, 0x3, 0x0, 0x0, 0x3}, 0x0)`,
   142  		},
   143  		{
   144  			In:  `sched_setattr(0x0, 0x123456, 0x0)`,
   145  			Out: `sched_setattr(0x0, 0x0, 0x0)`,
   146  		},
   147  		{
   148  			In:        `sched_setattr(0x0, &(0x7f00000001c0)=ANY=[@ANYBLOB="1234567812345678"], 0x0)`,
   149  			Out:       `sched_setattr(0x0, &(0x7f00000001c0)={0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 0x0)`,
   150  			StrictErr: `wrong array arg`,
   151  		},
   152  	})
   153  }
   154  
   155  func TestDeserializeStrictUnsafe(t *testing.T) {
   156  	t.Parallel()
   157  	target, _ := prog.GetTarget("linux", "amd64")
   158  	// Raw mode must preserve the global file name, allow to use mmap with non-fixed addr,
   159  	// and allow to use disabled syscalls.
   160  	had := `openat(0x0, &(0x7f0000000000)='/dev/foo', 0x0, 0x0)
   161  mmap(0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
   162  clone(0x0, &(0x7f0000000000), &(0x7f0000000010), &(0x7f0000000020), &(0x7f0000000030))
   163  `
   164  	p, err := target.Deserialize([]byte(had), prog.StrictUnsafe)
   165  	if err != nil {
   166  		t.Fatal(err)
   167  	}
   168  	got := string(p.Serialize())
   169  	if had != got {
   170  		t.Fatalf("program was changed:\n%s\ngot:\n%s", had, got)
   171  	}
   172  }
   173  
   174  func TestDeserializeNonStrictUnsafe(t *testing.T) {
   175  	t.Parallel()
   176  	target, _ := prog.GetTarget("linux", "amd64")
   177  	_, err := target.Deserialize([]byte("clone()"), prog.NonStrictUnsafe)
   178  	if err != nil {
   179  		t.Fatal(err)
   180  	}
   181  }