Skip to content

Commit ee18d64

Browse files
dhowellsxjamesmorris
authored andcommitted
KEYS: Add a keyctl to install a process's session keyring on its parent [try raspberrypi#6]
Add a keyctl to install a process's session keyring onto its parent. This replaces the parent's session keyring. Because the COW credential code does not permit one process to change another process's credentials directly, the change is deferred until userspace next starts executing again. Normally this will be after a wait*() syscall. To support this, three new security hooks have been provided: cred_alloc_blank() to allocate unset security creds, cred_transfer() to fill in the blank security creds and key_session_to_parent() - which asks the LSM if the process may replace its parent's session keyring. The replacement may only happen if the process has the same ownership details as its parent, and the process has LINK permission on the session keyring, and the session keyring is owned by the process, and the LSM permits it. Note that this requires alteration to each architecture's notify_resume path. This has been done for all arches barring blackfin, m68k* and xtensa, all of which need assembly alteration to support TIF_NOTIFY_RESUME. This allows the replacement to be performed at the point the parent process resumes userspace execution. This allows the userspace AFS pioctl emulation to fully emulate newpag() and the VIOCSETTOK and VIOCSETTOK2 pioctls, all of which require the ability to alter the parent process's PAG membership. However, since kAFS doesn't use PAGs per se, but rather dumps the keys into the session keyring, the session keyring of the parent must be replaced if, for example, VIOCSETTOK is passed the newpag flag. This can be tested with the following program: #include <stdio.h> #include <stdlib.h> #include <keyutils.h> #define KEYCTL_SESSION_TO_PARENT 18 #define OSERROR(X, S) do { if ((long)(X) == -1) { perror(S); exit(1); } } while(0) int main(int argc, char **argv) { key_serial_t keyring, key; long ret; keyring = keyctl_join_session_keyring(argv[1]); OSERROR(keyring, "keyctl_join_session_keyring"); key = add_key("user", "a", "b", 1, keyring); OSERROR(key, "add_key"); ret = keyctl(KEYCTL_SESSION_TO_PARENT); OSERROR(ret, "KEYCTL_SESSION_TO_PARENT"); return 0; } Compiled and linked with -lkeyutils, you should see something like: [dhowells@andromeda ~]$ keyctl show Session Keyring -3 --alswrv 4043 4043 keyring: _ses 355907932 --alswrv 4043 -1 \_ keyring: _uid.4043 [dhowells@andromeda ~]$ /tmp/newpag [dhowells@andromeda ~]$ keyctl show Session Keyring -3 --alswrv 4043 4043 keyring: _ses 1055658746 --alswrv 4043 4043 \_ user: a [dhowells@andromeda ~]$ /tmp/newpag hello [dhowells@andromeda ~]$ keyctl show Session Keyring -3 --alswrv 4043 4043 keyring: hello 340417692 --alswrv 4043 4043 \_ user: a Where the test program creates a new session keyring, sticks a user key named 'a' into it and then installs it on its parent. Signed-off-by: David Howells <dhowells@redhat.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent d0420c8 commit ee18d64

34 files changed

Lines changed: 409 additions & 0 deletions

File tree

‎Documentation/keys.txt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,26 @@ The keyctl syscall functions are:
757757
successful.
758758

759759

760+
(*) Install the calling process's session keyring on its parent.
761+
762+
long keyctl(KEYCTL_SESSION_TO_PARENT);
763+
764+
This functions attempts to install the calling process's session keyring
765+
on to the calling process's parent, replacing the parent's current session
766+
keyring.
767+
768+
The calling process must have the same ownership as its parent, the
769+
keyring must have the same ownership as the calling process, the calling
770+
process must have LINK permission on the keyring and the active LSM module
771+
mustn't deny permission, otherwise error EPERM will be returned.
772+
773+
Error ENOMEM will be returned if there was insufficient memory to complete
774+
the operation, otherwise 0 will be returned to indicate success.
775+
776+
The keyring will be replaced next time the parent process leaves the
777+
kernel and resumes executing userspace.
778+
779+
760780
===============
761781
KERNEL SERVICES
762782
===============

‎arch/alpha/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,5 +687,7 @@ do_notify_resume(struct pt_regs *regs, struct switch_stack *sw,
687687
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
688688
clear_thread_flag(TIF_NOTIFY_RESUME);
689689
tracehook_notify_resume(regs);
690+
if (current->replacement_session_keyring)
691+
key_replace_session_keyring();
690692
}
691693
}

‎arch/arm/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,5 +711,7 @@ do_notify_resume(struct pt_regs *regs, unsigned int thread_flags, int syscall)
711711
if (thread_flags & _TIF_NOTIFY_RESUME) {
712712
clear_thread_flag(TIF_NOTIFY_RESUME);
713713
tracehook_notify_resume(regs);
714+
if (current->replacement_session_keyring)
715+
key_replace_session_keyring();
714716
}
715717
}

‎arch/avr32/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,5 +326,7 @@ asmlinkage void do_notify_resume(struct pt_regs *regs, struct thread_info *ti)
326326
if (ti->flags & _TIF_NOTIFY_RESUME) {
327327
clear_thread_flag(TIF_NOTIFY_RESUME);
328328
tracehook_notify_resume(regs);
329+
if (current->replacement_session_keyring)
330+
key_replace_session_keyring();
329331
}
330332
}

‎arch/cris/kernel/ptrace.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ void do_notify_resume(int canrestart, struct pt_regs *regs,
4040
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
4141
clear_thread_flag(TIF_NOTIFY_RESUME);
4242
tracehook_notify_resume(regs);
43+
if (current->replacement_session_keyring)
44+
key_replace_session_keyring();
4345
}
4446
}

‎arch/frv/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ asmlinkage void do_notify_resume(__u32 thread_info_flags)
572572
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
573573
clear_thread_flag(TIF_NOTIFY_RESUME);
574574
tracehook_notify_resume(__frame);
575+
if (current->replacement_session_keyring)
576+
key_replace_session_keyring();
575577
}
576578

577579
} /* end do_notify_resume() */

‎arch/h8300/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,5 +556,7 @@ asmlinkage void do_notify_resume(struct pt_regs *regs, u32 thread_info_flags)
556556
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
557557
clear_thread_flag(TIF_NOTIFY_RESUME);
558558
tracehook_notify_resume(regs);
559+
if (current->replacement_session_keyring)
560+
key_replace_session_keyring();
559561
}
560562
}

‎arch/ia64/kernel/process.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ do_notify_resume_user(sigset_t *unused, struct sigscratch *scr, long in_syscall)
192192
if (test_thread_flag(TIF_NOTIFY_RESUME)) {
193193
clear_thread_flag(TIF_NOTIFY_RESUME);
194194
tracehook_notify_resume(&scr->pt);
195+
if (current->replacement_session_keyring)
196+
key_replace_session_keyring();
195197
}
196198

197199
/* copy user rbs to kernel rbs */

‎arch/m32r/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,8 @@ void do_notify_resume(struct pt_regs *regs, sigset_t *oldset,
411411
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
412412
clear_thread_flag(TIF_NOTIFY_RESUME);
413413
tracehook_notify_resume(regs);
414+
if (current->replacement_session_keyring)
415+
key_replace_session_keyring();
414416
}
415417

416418
clear_thread_flag(TIF_IRET);

‎arch/mips/kernel/signal.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,5 +704,7 @@ asmlinkage void do_notify_resume(struct pt_regs *regs, void *unused,
704704
if (thread_info_flags & _TIF_NOTIFY_RESUME) {
705705
clear_thread_flag(TIF_NOTIFY_RESUME);
706706
tracehook_notify_resume(regs);
707+
if (current->replacement_session_keyring)
708+
key_replace_session_keyring();
707709
}
708710
}

0 commit comments

Comments
 (0)