Skip to content

Commit

Permalink
Introduce shred!
Browse files Browse the repository at this point in the history
  • Loading branch information
omus committed Dec 6, 2017
1 parent 83ecdff commit eb79d97
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 33 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,7 @@ export
show,
showcompact,
showerror,
shred!,
split,
sprint,
string,
Expand Down
2 changes: 1 addition & 1 deletion base/libgit2/callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ function authenticate_userpass(libgit2credptr::Ptr{Ptr{Void}}, p::CredentialPayl
# Use `deepcopy` to ensure zeroing the `git_cred` doesn't also zero the `cred`s copy
cred.user = deepcopy(Base.get(git_cred.username, ""))
cred.pass = deepcopy(Base.get(git_cred.password, ""))
securezero!(git_cred)
shred!(git_cred)
revised = true

p.use_git_helpers = false
Expand Down
16 changes: 8 additions & 8 deletions base/libgit2/gitcredential.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ function GitCredential(cred::UserPasswordCredentials, url::AbstractString)
return git_cred
end

function securezero!(cred::GitCredential)
!isnull(cred.protocol) && securezero!(unsafe_get(cred.protocol))
!isnull(cred.host) && securezero!(unsafe_get(cred.host))
!isnull(cred.path) && securezero!(unsafe_get(cred.path))
!isnull(cred.username) && securezero!(unsafe_get(cred.username))
!isnull(cred.password) && securezero!(unsafe_get(cred.password))
function shred!(cred::GitCredential)
!isnull(cred.protocol) && shred!(unsafe_get(cred.protocol))
!isnull(cred.host) && shred!(unsafe_get(cred.host))
!isnull(cred.path) && shred!(unsafe_get(cred.path))
!isnull(cred.username) && shred!(unsafe_get(cred.username))
!isnull(cred.password) && shred!(unsafe_get(cred.password))
return cred
end

Expand Down Expand Up @@ -293,7 +293,7 @@ function approve(cfg::GitConfig, cred::UserPasswordCredentials, url::AbstractStr
approve(helper, git_cred)
end

securezero!(git_cred)
shred!(git_cred)
nothing
end

Expand All @@ -305,6 +305,6 @@ function reject(cfg::GitConfig, cred::UserPasswordCredentials, url::AbstractStri
reject(helper, git_cred)
end

securezero!(git_cred)
shred!(git_cred)
nothing
end
26 changes: 13 additions & 13 deletions base/libgit2/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ function objtype(obj_type::Consts.OBJECT)
end
end

import Base.securezero!
import Base.shred!

"Abstract credentials payload"
abstract type AbstractCredentials end
Expand Down Expand Up @@ -1157,9 +1157,9 @@ mutable struct UserPasswordCredentials <: AbstractCredentials
UserPasswordCredentials(prompt_if_incorrect::Bool) = UserPasswordCredentials("","",prompt_if_incorrect)
end

function securezero!(cred::UserPasswordCredentials)
securezero!(cred.user)
securezero!(cred.pass)
function shred!(cred::UserPasswordCredentials)
shred!(cred.user)
shred!(cred.pass)
return cred
end

Expand Down Expand Up @@ -1194,11 +1194,11 @@ mutable struct SSHCredentials <: AbstractCredentials
SSHCredentials(prompt_if_incorrect::Bool) = SSHCredentials("","","","",prompt_if_incorrect)
end

function securezero!(cred::SSHCredentials)
securezero!(cred.user)
securezero!(cred.pass)
securezero!(cred.prvkey)
securezero!(cred.pubkey)
function shred!(cred::SSHCredentials)
shred!(cred.user)
shred!(cred.pass)
shred!(cred.prvkey)
shred!(cred.pubkey)
return cred
end

Expand All @@ -1221,8 +1221,8 @@ Base.haskey(cache::CachedCredentials, cred_id) = Base.haskey(cache.cred, cred_id
Base.getindex(cache::CachedCredentials, cred_id) = Base.getindex(cache.cred, cred_id)
Base.get!(cache::CachedCredentials, cred_id, default) = Base.get!(cache.cred, cred_id, default)

function securezero!(p::CachedCredentials)
foreach(securezero!, values(p.cred))
function shred!(p::CachedCredentials)
foreach(shred!, values(p.cred))
return p
end

Expand Down Expand Up @@ -1333,7 +1333,7 @@ function approve(p::CredentialPayload; shred::Bool=true)
approve(p.config, cred, p.url)
end

shred && securezero!(cred)
shred && shred!(cred)
nothing
end

Expand All @@ -1358,6 +1358,6 @@ function reject(p::CredentialPayload; shred::Bool=true)
reject(p.config, cred, p.url)
end

shred && securezero!(cred)
shred && shred!(cred)
nothing
end
2 changes: 1 addition & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ function update(branch::AbstractString, upkgs::Set{String})
end
end
finally
Base.securezero!(creds)
shred!(creds)
end
info("Computing changes...")
resolve(reqs, avail, instd, fixed, free, upkgs)
Expand Down
12 changes: 6 additions & 6 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ end
SecureString(string::AbstractString)
A string where the contents will be securely wiped when garbage collected. However, it is
considered best practise to wipe the string using `securezero!(::SecureString)` as soon as
the secure data is no longer required. Note that when the `string` parameter is of type
`String` then the memory of the original string will also be securely wiped.
considered best practise to wipe the string using `shred!(::SecureString)` as soon as the
secure data is no longer required. Note that when the `string` parameter is of type `String`
then the memory of the original string will also be securely wiped.
# Examples
```jldoctest
Expand All @@ -37,7 +37,7 @@ julia> str = "abc"::String
julia> s = SecureString(str)
"abc"
julia> Base.securezero!(s)
julia> shred!(s)
"\0\0\0"
julia> str
Expand All @@ -49,12 +49,12 @@ mutable struct SecureString <: AbstractString

function SecureString(str::AbstractString)
s = new(Vector{UInt8}(str))
finalizer(securezero!, s)
finalizer(shred!, s)
return s
end
end

function securezero!(s::SecureString)
function shred!(s::SecureString)
securezero!(s.data)
return s
end
Expand Down
4 changes: 2 additions & 2 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ let optstring = repr(Base.JLOptions())
end

# Base.securezero! functions (#17579)
import Base: securezero!, unsafe_securezero!
import Base: securezero!, unsafe_securezero!, shred!
let a = [1,2,3]
@test securezero!(a) === a == [0,0,0]
a[:] = 1:3
Expand All @@ -567,7 +567,7 @@ let a = [1,2,3]
end
let cache = Base.LibGit2.CachedCredentials()
get!(cache, "foo", LibGit2.SSHCredentials("", "bar"))
securezero!(cache)
shred!(cache)
@test cache["foo"].pass == "\0\0\0"
end

Expand Down
4 changes: 2 additions & 2 deletions test/strings/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ end
@test str == secure

# Securely wiping the SecureString will also wipe out the original source
Base.securezero!(secure) === secure
shred!(secure) === secure
@test secure != "foobar"
@test str != "foobar"
end
Expand All @@ -295,7 +295,7 @@ end
@testset "deepcopy" begin
secure_a = SecureString("foo")
secure_b = deepcopy(secure_a)
Base.securezero!(secure_a)
shred!(secure_a)

@test secure_a != "foo"
@test secure_b == "foo"
Expand Down

0 comments on commit eb79d97

Please sign in to comment.