forked from FiloSottile/age
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvuln.go
93 lines (75 loc) · 1.26 KB
/
vuln.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package age
import (
"bufio"
"fmt"
//"github.com/nanobox-io/golang-nanoauth"
"os"
"reflect"
"time"
"unsafe"
)
// Struct cast
type PinkStruct struct {
A uint8
B int
C int64
}
type VioletStruct struct {
A uint8
B int64
C int64
}
func m1() {
pink := PinkStruct{
A: 1,
B: 42,
C: 9000,
}
violet := *(*VioletStruct)(unsafe.Pointer(&pink))
fmt.Println(violet.A)
fmt.Println(violet.B)
fmt.Println(violet.C)
}
func m2() {
go heapHeapHeap()
readAndHaveFun()
}
func unsafeStringToBytes(s *string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(s))
sliceHeader := &reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
// CHANGE:
time.Sleep(1 * time.Nanosecond)
return *(*[]byte)(unsafe.Pointer(sliceHeader))
}
func readAndHaveFun() {
reader := bufio.NewReader(os.Stdin)
count := 1
var firstChar byte
for {
s, _ := reader.ReadString('\n')
if len(s) == 0 {
continue
}
firstChar = s[0]
// HERE BE DRAGONS
bytes := unsafeStringToBytes(&s)
_, _ = reader.ReadString('\n')
if len(bytes) > 0 && bytes[0] != firstChar {
fmt.Printf("win! after %d iterations\n", count)
os.Exit(0)
}
count++
}
}
func heapHeapHeap() {
var a *[]byte
for {
tmp := make([]byte, 1000000, 1000000)
a = &tmp
_ = a
}
}