Skip to content

Instantly share code, notes, and snippets.

@line-o
Last active January 19, 2023 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save line-o/b0058ee7a61ff783e6f6ba9c13e0434f to your computer and use it in GitHub Desktop.
Save line-o/b0058ee7a61ff783e6f6ba9c13e0434f to your computer and use it in GitHub Desktop.
fun with permissions
0600 (.rw-------) -> 0700 (.rwx------)
0755 (.rwxr-xr-x) -> 0755 (.rwxr-xr-x)
0660 (.rw-rw----) -> 0770 (.rwxrwx---)
0664 (.rw-rw-r--) -> 0775 (.rwxrwxr-x)
0666 (.rw-rw-rw-) -> 0777 (.rwxrwxrwx)
0622 (.rw--w--w-) -> 0733 (.rwx-wx-wx)
0777 (.rwxrwxrwx) -> 0777 (.rwxrwxrwx)
class Main {
static int ownerRead = 04 << 6;
static int ownerWrite = 02 << 6;
static int ownerExecute = 01 << 6;
static int groupRead = 04 << 3;
static int groupWrite = 02 << 3;
static int groupExecute = 01 << 3;
static int otherRead = 04;
static int otherWrite = 02;
static int otherExecute = 01;
static int noop = 00;
public static void main(String args[]) {
checkPermElevation("0600");
checkPermElevation("0755");
checkPermElevation("0660");
checkPermElevation("0664");
checkPermElevation("0666");
checkPermElevation("0622");
checkPermElevation("0777");
}
static void checkPermElevation (final String oct) {
int p = Integer.parseInt(oct, 8);
int elevated = safeElevatePrivileges(p);
System.out.println(oct + " (" + toStringRepresentation(p) + ") -> 0" + Integer.toOctalString(elevated) + " (" + toStringRepresentation(elevated) + ")");
}
static char check(final int subject, final int octal, final char successValue) {
return ((subject & octal) > 0) ? successValue : '-';
}
static String toStringRepresentation (final int p) {
return "." +
check(p, ownerRead, 'r') +
check(p, ownerWrite, 'w') +
check(p, ownerExecute, 'x') +
check(p, groupRead, 'r') +
check(p, groupWrite, 'w') +
check(p, groupExecute, 'x') +
check(p, otherRead, 'r') +
check(p, otherWrite, 'w') +
check(p, otherExecute, 'x')
;
}
static int setExecutableIfOtherCanReadOrWrite (final int p) {
final boolean canReadOrWrite = (p & otherRead) + (p & otherWrite) > 0;
return canReadOrWrite ? otherExecute : noop;
}
static int setExecutableIfGroupCanReadOrWrite (final int p) {
final boolean canReadOrWrite = (p & groupRead) + (p & groupWrite) > 0;
return canReadOrWrite ? groupExecute : noop;
}
static int setExecutableForOwner (final int p) {
return ownerExecute;
}
static int safeElevatePrivileges (final int p) {
return p
| ownerExecute
| setExecutableIfGroupCanReadOrWrite(p)
| setExecutableIfOtherCanReadOrWrite(p)
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment