Skip to content

Commit

Permalink
8274721: UnixSystem fails to provide uid, gid or groups if no usernam…
Browse files Browse the repository at this point in the history
…e is available

Reviewed-by: mullan
  • Loading branch information
wangweij committed Oct 18, 2021
1 parent 1afddb2 commit a619f89
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions src/jdk.security.auth/unix/native/libjaas/Unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,54 +66,63 @@ Java_com_sun_security_auth_module_UnixSystem_getUnixInfo
groups = (gid_t *)calloc(numSuppGroups, sizeof(gid_t));
if (groups == NULL) {
jclass cls = (*env)->FindClass(env,"java/lang/OutOfMemoryError");
if (cls != NULL)
if (cls != NULL) {
(*env)->ThrowNew(env, cls, NULL);
}
return;
}

cls = (*env)->GetObjectClass(env, obj);

memset(pwd_buf, 0, sizeof(pwd_buf));

if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
pwd != NULL &&
getgroups(numSuppGroups, groups) != -1) {
supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
if (supplementaryGroupID == 0) {
goto cleanUpAndReturn;
}

userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
if (userNameID == 0)
if (getgroups(numSuppGroups, groups) != -1) {
jgroups = (*env)->NewLongArray(env, numSuppGroups);
if (jgroups == NULL) {
goto cleanUpAndReturn;

userID = (*env)->GetFieldID(env, cls, "uid", "J");
if (userID == 0)
}
jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
if (jgroupsAsArray == NULL) {
goto cleanUpAndReturn;
}
for (i = 0; i < numSuppGroups; i++) {
jgroupsAsArray[i] = groups[i];
}
(*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
(*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
}

groupID = (*env)->GetFieldID(env, cls, "gid", "J");
if (groupID == 0)
goto cleanUpAndReturn;
userNameID = (*env)->GetFieldID(env, cls, "username", "Ljava/lang/String;");
if (userNameID == 0) {
goto cleanUpAndReturn;
}

supplementaryGroupID = (*env)->GetFieldID(env, cls, "groups", "[J");
if (supplementaryGroupID == 0)
goto cleanUpAndReturn;
userID = (*env)->GetFieldID(env, cls, "uid", "J");
if (userID == 0) {
goto cleanUpAndReturn;
}

jstr = (*env)->NewStringUTF(env, pwd->pw_name);
if (jstr == NULL)
goto cleanUpAndReturn;
(*env)->SetObjectField(env, obj, userNameID, jstr);
groupID = (*env)->GetFieldID(env, cls, "gid", "J");
if (groupID == 0) {
goto cleanUpAndReturn;
}

memset(pwd_buf, 0, sizeof(pwd_buf));
if (getpwuid_r(getuid(), &resbuf, pwd_buf, sizeof(pwd_buf), &pwd) == 0 &&
pwd != NULL) {
(*env)->SetLongField(env, obj, userID, pwd->pw_uid);

(*env)->SetLongField(env, obj, groupID, pwd->pw_gid);

jgroups = (*env)->NewLongArray(env, numSuppGroups);
if (jgroups == NULL)
goto cleanUpAndReturn;
jgroupsAsArray = (*env)->GetLongArrayElements(env, jgroups, 0);
if (jgroupsAsArray == NULL)
jstr = (*env)->NewStringUTF(env, pwd->pw_name);
if (jstr == NULL) {
goto cleanUpAndReturn;
for (i = 0; i < numSuppGroups; i++)
jgroupsAsArray[i] = groups[i];
(*env)->ReleaseLongArrayElements(env, jgroups, jgroupsAsArray, 0);
(*env)->SetObjectField(env, obj, supplementaryGroupID, jgroups);
}
(*env)->SetObjectField(env, obj, userNameID, jstr);
} else {
(*env)->SetLongField(env, obj, userID, getuid());
(*env)->SetLongField(env, obj, groupID, getgid());
}
cleanUpAndReturn:
free(groups);
Expand Down

1 comment on commit a619f89

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.