gpt4 book ai didi

string - 是否可以将 Golang 字符串的内存 "safely"归零?

转载 作者:IT王子 更新时间:2023-10-28 23:36:55 28 4
gpt4 key购买 nike

最近我一直在使用 cgo 在我的一个项目中设置 libsodium,以便使用 crypto_pwhash_strcrypto_pwhash_str_verify 函数.

这一切都进行得非常顺利,我现在有一小部分函数,​​它们以纯文本密码的形式接收 []byte 并将其散列,或将其与另一个进行比较[]byte 进行验证。

我使用 []byte 而不是 string 的原因是,根据我目前所学到的有关 Go 的知识,我至少可以遍历纯文本密码和零所有字节,甚至将指针传递给 libsodiumsodium_memzero 函数,以免它在内存中停留的时间比它长需要。

这对于我能够将输入直接读取为字节的应用程序来说很好,但我现在正尝试在一个小型 Web 应用程序中使用它,我需要使用 POST< 从表单中读取密码 方法。

从我在 Go 源代码和文档中可以看到,在请求处理程序中使用 r.ParseForm 会将所有表单值解析为 map字符串s.

问题在于,因为 Go 中的 string 是不可变的,所以我认为我无法将 POST 中的密码的内存归零形式;至少,只使用 Go。

所以看来我唯一(简单)的选择是将 unsafe.Pointer 连同字节数一起传递给 C 中的函数,然后让 C 为我将内存归零(对于例如,将其传递给上述 sodium_memzero 函数)。

我已经尝试过了,不出所料,它当然可以工作,但是我在 Go 中留下了一个不安全的 string,如果在像 fmt.Println 这样的函数中使用它 会使程序崩溃。

我的问题如下:

  • 我是否应该接受密码将被 POST 编辑并解析为字符串,并且我不应该弄乱它并等待 GC 启动? (不理想)
  • 使用 cgo 将 string 的内存归零好吗,前提是代码中明显记录了不应再次使用字符串变量?
  • 使用 cgo 将 string 的内存归零会导致 GC 崩溃吗?
  • 是否值得为 http.Request 编写一种装饰器,它添加了一个函数以将表单值直接解析为 []byte 以便我可以完全控制这些值他们什么时候到达?

编辑:为了澄清,网络应用程序和表单 POST 只是一个方便的例子,我可能会因为使用 Go 的标准库而收到敏感数据string 的形式。我更感兴趣的是我的所有问题是否可能/是否值得在某些情况下尽快清理内存中的数据更多的是安全问题。

最佳答案

鉴于在这个问题上似乎没有太多事件,我将假设大多数人以前不需要/不想研究这个问题,或者不认为值得花时间.因此,尽管我对 Go 的内部运作一无所知,但我只会发布我自己的发现作为答案。

我应该在这个答案的开头加上免责声明,因为 Go 是一种垃圾收集语言,而且我不知道它在内部是如何工作的,所以以下信息实际上可能根本不能保证任何内存实际上被清除为零,但这不会不要阻止我尝试;毕竟,在我看来,内存中的纯文本密码越少越好。

考虑到这一点,这就是我发现(据我所知)与 libsodium 一起工作的所有内容;到目前为止,至少我的任何程序都没有崩溃。

首先,你可能已经知道 Go 中的 string 是不可变的,所以技术上它们的值不应该改变,但是如果我们使用 unsafe.Pointer 到 Go 中的 string 或通过 Cgo 在 C 中的 string,我们实际上可以覆盖 string 值中存储的数据;我们只是不能保证在内存中的其他任何地方都没有任何其他数据副本。

出于这个原因,我让与密码相关的函数专门处理 []byte 变量,以减少可能在内存中复制的纯文本密码的数量。

我还返回 []byte 引用,用于传递给所有密码函数的纯文本密码,因为将 string 转换为 []byte 将分配新内存并复制内容。这样,至少如果您将 string 就地转换为 []byte 而不先将其分配给变量,您仍然可以访问新的 []byte 在函数调用完成后将内存归零。

以下是我想出的要点。您可以填写空白,包含 libsodium C 库并编译它以自己查看结果。

对我来说,它在调用 MemZero* 函数之前输出:

pwd     : Correct Horse Battery Staple
pwdBytes: [67 111 114 114 101 99 116 32 72 111 114 115 101 32 66 97 116 116 101 114 121 32 83 116 97 112 108 101]

然后在调用 MemZero* 函数之后:

pwd     :
pwdBytes: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Hash: $argon2i$v=19$m=131072,t=6,p=1$N05osI8nuTjftzfAYBIcbA$3yb92yt9S9dRmPtlSV/J8jY4DG3reqm+2eV+fi54Its

所以看起来很成功,但由于我们不能保证内存中其他地方没有纯文本密码的副本,我认为这是我们可以进行的。

下面的代码只是将带有 byte 数量的 unsafe.Pointer 传递给 C 中的 sodium_memzero 函数来实现这一点。所以实际的内存归零由 libsodium 决定。

如果我在代码中留下任何拼写错误或任何不起作用的内容,我深表歉意,但我不想粘贴太多,只粘贴相关部分。

例如,如果你真的需要,你也可以使用像 mlock 这样的函数,但由于这个问题的重点是清零 string 我将只展示在这里。

package sodium

// Various imports, other functions and <sodium.h> here...

func init() {
if err := sodium.Init(); err != nil {
log.Fatalf("sodium: %s", err)
}
}

func PasswordHash(pwd []byte, opslimit, memlimit int) ([]byte, []byte, error) {
pwdPtr := unsafe.Pointer(&pwd[0])
hashPtr := unsafe.Pointer(&make([]byte, C.crypto_pwhash_STRBYTES)[0])

res := C.crypto_pwhash_str(
(*C.char)(hashPtr),
(*C.char)(pwdPtr),
C.ulonglong(len(pwd)),
C.ulonglong(opslimit),
C.size_t(memlimit),
)
if res != 0 {
return nil, pwd, fmt.Errorf("sodium: passwordhash: out of memory")
}
return C.GoBytes(hashPtr, C.crypto_pwhash_STRBYTES), pwd, nil
}

func MemZero(p unsafe.Pointer, size int) {
if p != nil && size > 0 {
C.sodium_memzero(p, C.size_t(size))
}
}

func MemZeroBytes(bytes []byte) {
if size := len(bytes); size > 0 {
MemZero(unsafe.Pointer(&bytes[0]), size)
}
}

func MemZeroStr(str *string) {
if size := len(*str); size > 0 {
MemZero(unsafe.Pointer(str), size)
}
}

然后全部使用:

package main

// Imports etc here...

func main() {
// Unfortunately there is no guarantee that this won't be
// stored elsewhere in memory, but we will try to remove it anyway
pwd := "Correct Horse Battery Staple"

// I convert the pwd string to a []byte in place here
// Because of this I have no reference to the new memory, with yet
// another copy of the plain password hanging around
// The function always returns the new []byte as the second value
// though, so we can still zero it anyway
hash, pwdBytes, err := sodium.PasswordHash([]byte(pwd), 6, 134217728)

// Byte slice and string before MemZero* functions
fmt.Println("pwd :", pwd)
fmt.Println("pwdBytes:", pwdBytes)

// No need to keep a plain-text password in memory any longer than required
sodium.MemZeroStr(&pwd)
sodium.MemZeroBytes(pwdBytes)
if err != nil {
log.Fatal(err)
}

// Byte slice and string after MemZero* functions
fmt.Println("pwd :", pwd)
fmt.Println("pwdBytes:", pwdBytes)

// We've done our best to make sure we only have the hash in memory now
fmt.Println("Hash:", string(hash))
}

关于string - 是否可以将 Golang 字符串的内存 "safely"归零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39968084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com