Skip to content
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

OWS-644, ITW 955 : fix restricted file ACL #965

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclEntryFlag;
import java.nio.file.attribute.AclEntryPermission;
import java.nio.file.attribute.AclEntryType;
import java.nio.file.attribute.AclFileAttributeView;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static java.lang.Boolean.parseBoolean;
Expand All @@ -49,6 +52,8 @@ public final class RestrictedFileUtils {
private static final Logger LOG = LoggerFactory.getLogger(RestrictedFileUtils.class);

private static final List<String> WIN_ROOT_PRINCIPALS = Arrays.asList("NT AUTHORITY\\SYSTEM", "BUILTIN\\Administrators");
private static final List<String> WIN_PRINCIPAL_SIDS = Arrays.asList("S-1-5-18" /*NT AUTHORITY\SYSTEM*/, "S-1-5-32-544" /*BUILTIN\Administrators*/, "S-1-5-11" /*NT AUTHORITY\Authenticated Users*/);
public static final String NT_AUTHENTICATED_USER_SID = "S-1-5-11";

/**
* Creates a new directory with minimum permissions. The directory is not
Expand Down Expand Up @@ -125,7 +130,8 @@ private static void createRestrictedFile(File file, boolean isDir) throws IOExce
String owner = view.getOwner().getName();
for (AclEntry ae : view.getAcl()) {
String principalName = ae.principal().getName();
if (WIN_ROOT_PRINCIPALS.contains(principalName) || owner.equals(principalName)) {
if (WIN_ROOT_PRINCIPALS.contains(principalName) || owner.equals(principalName) || principalInWinSIDS(ae.principal())) {
LOG.debug("Allowing permissions on restricted file {} for principal {} : {} ", tempFile.getAbsolutePath(), principalName, getSIDForPrincipal(ae.principal()));
list.add(AclEntry.newBuilder()
.setType(AclEntryType.ALLOW)
.setPrincipal(ae.principal())
Expand Down Expand Up @@ -189,4 +195,22 @@ private static void createFileOrDir(File file, boolean isDir) throws IOException
}
}
}

public static boolean principalInWinSIDS(Principal principal) {
return getSIDForPrincipal(principal)
.map(WIN_PRINCIPAL_SIDS::contains)
.orElse(false);
}

public static Optional<String> getSIDForPrincipal(Principal principal) {
try {
Class<?> clazz = principal.getClass().getSuperclass();
Method method = clazz.getDeclaredMethod("sidString");
method.setAccessible(true);
return Optional.ofNullable((String) method.invoke(principal));
} catch (Exception e) {
LOG.debug("No SID for {}", principal.getName());
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.attribute.AclEntry;
import java.nio.file.attribute.AclFileAttributeView;

import static net.sourceforge.jnlp.util.RestrictedFileUtils.getSIDForPrincipal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand All @@ -32,7 +33,7 @@ public void testCreateRestrictedFile() throws Exception {
boolean hasOwner = false;
AclFileAttributeView view = Files.getFileAttributeView(testfile.toPath(), AclFileAttributeView.class);
for (AclEntry ae : view.getAcl()) {
if (view.getOwner().getName().equals(ae.principal().getName())) {
if (view.getOwner().getName().equals(ae.principal().getName()) || getSIDForPrincipal(ae.principal()).orElse("").equals(RestrictedFileUtils.NT_AUTHENTICATED_USER_SID)) {
assertFalse("Duplicate owner entry", hasOwner);
hasOwner = true;
assertEquals("Owner must have all permissions", 14, ae.permissions().size());
Expand Down
Loading