-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8277489: Rewrite JAAS UnixLoginModule with FFM #28931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| /* | ||
| * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved. | ||
| * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
|
|
@@ -25,6 +25,14 @@ | |
|
|
||
| package com.sun.security.auth.module; | ||
|
|
||
| import jdk.internal.ffi.generated.jaas_unix.passwd; | ||
|
|
||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.lang.foreign.ValueLayout; | ||
|
|
||
| import static jdk.internal.ffi.generated.jaas_unix.jaas_unix_h.*; | ||
|
|
||
| /** | ||
| * This class implementation retrieves and makes available Unix | ||
| * UID/GID/groups information for the current user. | ||
|
|
@@ -33,10 +41,6 @@ | |
| */ | ||
| public class UnixSystem { | ||
|
|
||
| private native void getUnixInfo(); | ||
|
|
||
| // Warning: the following 4 fields are used by Unix.c | ||
|
|
||
| /** The current username. */ | ||
| protected String username; | ||
|
|
||
|
|
@@ -53,10 +57,42 @@ public class UnixSystem { | |
| * Instantiate a {@code UnixSystem} and load | ||
| * the native library to access the underlying system information. | ||
| */ | ||
| @SuppressWarnings("restricted") | ||
| public UnixSystem() { | ||
| System.loadLibrary("jaas"); | ||
| getUnixInfo(); | ||
| try (Arena scope = Arena.ofConfined()) { | ||
| int groupnum = getgroups(0, MemorySegment.NULL); | ||
| if (groupnum == -1) { | ||
| throw new RuntimeException("getgroups returns " + groupnum); | ||
| } | ||
|
|
||
| var gs = scope.allocate(gid_t, groupnum); | ||
|
||
| groupnum = getgroups(groupnum, gs); | ||
| if (groupnum == -1) { | ||
| throw new RuntimeException("getgroups returns " + groupnum); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a must-have, but would be nice to have a FFM for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. |
||
| } | ||
|
|
||
| groups = new long[groupnum]; | ||
| for (int i = 0; i < groupnum; i++) { | ||
| groups[i] = gs.getAtIndex(gid_t, i); | ||
| } | ||
|
|
||
| var resbuf = passwd.allocate(scope); | ||
|
||
| var pwd = scope.allocate(C_POINTER); | ||
| var pwd_buf = scope.allocate(1024); | ||
| int out = getpwuid_r(getuid(), resbuf, pwd_buf, pwd_buf.byteSize(), pwd); | ||
|
||
| if (out != 0) { | ||
| throw new RuntimeException("getpwuid_r returns " + out); | ||
| } | ||
| if (pwd.get(ValueLayout.ADDRESS, 0).equals(MemorySegment.NULL)) { | ||
| throw new RuntimeException("getpwuid_r returns NULL result"); | ||
| } | ||
| uid = passwd.pw_uid(resbuf); | ||
| gid = passwd.pw_gid(resbuf); | ||
| username = passwd.pw_name(resbuf).getString(0); | ||
| } catch (Throwable t) { | ||
| var error = new UnsatisfiedLinkError("FFM calls failed"); | ||
| error.initCause(t); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to check with getCause() here as well?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. | ||
| * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
| * | ||
| * This code is free software; you can redistribute it and/or modify it | ||
| * under the terms of the GNU General Public License version 2 only, as | ||
| * published by the Free Software Foundation. Oracle designates this | ||
| * particular file as subject to the "Classpath" exception as provided | ||
| * by Oracle in the LICENSE file that accompanied this code. | ||
| * | ||
| * This code is distributed in the hope that it will be useful, but WITHOUT | ||
| * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
| * version 2 for more details (a copy is included in the LICENSE file that | ||
| * accompanied this code). | ||
| * | ||
| * You should have received a copy of the GNU General Public License version | ||
| * 2 along with this work; if not, write to the Free Software Foundation, | ||
| * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
| * | ||
| * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| * or visit www.oracle.com if you need additional information or have any | ||
| * questions. | ||
| */ | ||
|
|
||
| // Generated by jextract | ||
|
|
||
| package jdk.internal.ffi.generated.jaas_unix; | ||
|
|
||
| import java.lang.invoke.*; | ||
| import java.lang.foreign.*; | ||
| import java.nio.ByteOrder; | ||
| import java.util.*; | ||
| import java.util.function.*; | ||
| import java.util.stream.*; | ||
|
|
||
| import static java.lang.foreign.ValueLayout.*; | ||
| import static java.lang.foreign.MemoryLayout.PathElement.*; | ||
|
|
||
| @SuppressWarnings("restricted") | ||
| public class jaas_unix_h$shared { | ||
|
|
||
| jaas_unix_h$shared() { | ||
| // Should not be called directly | ||
| } | ||
|
|
||
| public static final ValueLayout.OfBoolean C_BOOL = (ValueLayout.OfBoolean) Linker.nativeLinker().canonicalLayouts().get("bool"); | ||
| public static final ValueLayout.OfByte C_CHAR =(ValueLayout.OfByte)Linker.nativeLinker().canonicalLayouts().get("char"); | ||
| public static final ValueLayout.OfShort C_SHORT = (ValueLayout.OfShort) Linker.nativeLinker().canonicalLayouts().get("short"); | ||
| public static final ValueLayout.OfInt C_INT = (ValueLayout.OfInt) Linker.nativeLinker().canonicalLayouts().get("int"); | ||
| public static final ValueLayout.OfLong C_LONG_LONG = (ValueLayout.OfLong) Linker.nativeLinker().canonicalLayouts().get("long long"); | ||
| public static final ValueLayout.OfFloat C_FLOAT = (ValueLayout.OfFloat) Linker.nativeLinker().canonicalLayouts().get("float"); | ||
| public static final ValueLayout.OfDouble C_DOUBLE = (ValueLayout.OfDouble) Linker.nativeLinker().canonicalLayouts().get("double"); | ||
| public static final AddressLayout C_POINTER = ((AddressLayout) Linker.nativeLinker().canonicalLayouts().get("void*")) | ||
| .withTargetLayout(MemoryLayout.sequenceLayout(java.lang.Long.MAX_VALUE, C_CHAR)); | ||
| public static final ValueLayout.OfLong C_LONG = (ValueLayout.OfLong) Linker.nativeLinker().canonicalLayouts().get("long"); | ||
|
|
||
| static final boolean TRACE_DOWNCALLS = Boolean.getBoolean("jextract.trace.downcalls"); | ||
|
|
||
| static void traceDowncall(String name, Object... args) { | ||
| String traceArgs = Arrays.stream(args) | ||
| .map(Object::toString) | ||
| .collect(Collectors.joining(", ")); | ||
| System.out.printf("%s(%s)\n", name, traceArgs); | ||
| } | ||
|
|
||
| static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fdesc) { | ||
| try { | ||
| return MethodHandles.lookup().findVirtual(fi, name, fdesc.toMethodType()); | ||
| } catch (ReflectiveOperationException ex) { | ||
| throw new AssertionError(ex); | ||
| } | ||
| } | ||
|
|
||
| static MemoryLayout align(MemoryLayout layout, long align) { | ||
| return switch (layout) { | ||
| case PaddingLayout p -> p; | ||
| case ValueLayout v -> v.withByteAlignment(align); | ||
| case GroupLayout g -> { | ||
| MemoryLayout[] alignedMembers = g.memberLayouts().stream() | ||
| .map(m -> align(m, align)).toArray(MemoryLayout[]::new); | ||
| yield g instanceof StructLayout ? | ||
| MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers); | ||
| } | ||
| case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align)); | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a must-have, but would be nice to have a FFM for
errnoto provide better diagnostics.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea. There is a
Linker.Option.captureCallState("errno")feature and I'll see how to use it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there has been prior work with this, at least with macos, e.g.: src/java.base/macosx/classes/jdk/internal/ffi/generated/errno/errno_h.java