Why this Golang code could leak value of variable which is in memory

I

Iv4n

Guest
This code could leak the value of the variable which is in memory.

I think maybe the fmt.XprintY doesn't reset the buffer, but my attempt at debugging is fruitless.

package main

import (
"bytes"
"fmt"
"io"
"text/template"
)

type SecWriter struct {
w io.Writer
}

func (s *SecWriter) Write(p []byte) (n int, err error) {
fmt.Println(string(p), len(p), cap(p))

// here
tmp := fmt.Sprintln("info{SSSSSSSSSSSSSSSSSSSSSSSSSSS}")
if tmp == ""{}

s.w.Write(p[:64])
return 64, nil
}

func index() {
exp := "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA{{1}}"

b := &bytes.Buffer{}
s := &SecWriter{
w: b,
}


t := template.Must(template.New("index").Parse(exp))
t.Execute(s, nil)

fmt.Println("buf: ", b.String())
}

func main() {
index()
}


My go env:

set GOARCH=amd64
set GOOS=windows


go version

go version go1.12.5 windows/amd64


and the output is:

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 64 64
1 1 128
buf: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA1nfo{SSSSSSSSSSSSSSSSSSSSSSSSSSS} AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


As you can see, the value of the variable in memory leaks to the buffer.

Continue reading...
 
Top