Skip to content

Commit a265d6b

Browse files
committed
add support for kernel persistent keyring
See this PR for details: jsipprell#15
1 parent 68bb36e commit a265d6b

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

keyring.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Keyring interface {
2323
Search(string) (*Key, error)
2424
SearchType(string, string) (*Key, error)
2525
SetDefaultTimeout(uint)
26+
AttachPersistent() (Keyring, error)
2627
}
2728

2829
// Named keyrings are user-created keyrings linked to a parent keyring. The
@@ -103,6 +104,13 @@ func (kr *keyring) SearchType(name string, keyType string) (*Key, error) {
103104
return nil, err
104105
}
105106

107+
// AttachPersistent attaches the current executing context's persistent
108+
// keyring to this keyring. See persistent-keyring(7) for more info.
109+
// It returns either an error, or the persistent Keyring.
110+
func (kr *keyring) AttachPersistent() (Keyring, error) {
111+
return attachPersistent(kr.id)
112+
}
113+
106114
// Return the current login session keyring
107115
func SessionKeyring() (Keyring, error) {
108116
return newKeyring(keySpecSessionKeyring)

keyring_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ func TestCreateKeyring(t *testing.T) {
112112
}
113113
}
114114

115+
func TestAttachPersistentKeyring(t *testing.T) {
116+
kr, err := SessionKeyring()
117+
if err != nil {
118+
t.Fatalf("unexpected test failure: could not create session keyring: %v", err)
119+
}
120+
pkr, err := kr.AttachPersistent()
121+
if err != nil {
122+
t.Fatalf("unexpected test failure: could not attach persistent keyring: %v", err)
123+
}
124+
t.Logf("found persistent keyring %d", pkr.Id())
125+
}
126+
115127
func TestCreateNestedKeyring(t *testing.T) {
116128
ring := helperTestCreateKeyring(nil, "", t)
117129

sys_linux.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,12 @@ func keyctl_Move(id, from_ring keyId, to_ring keyId, flags uint) error {
354354
}
355355
return nil
356356
}
357+
358+
func attachPersistent(id keyId) (*keyring, error) {
359+
uid := int32(-1)
360+
r1, _, errno := syscall.Syscall(syscall_keyctl, uintptr(keyctlGetPersistent), uintptr(uid), uintptr(id))
361+
if errno != 0 {
362+
return nil, errno
363+
}
364+
return &keyring{id: keyId(r1)}, nil
365+
}

0 commit comments

Comments
 (0)