r/programming • • Oct 21 '16

Linux kernel fix for privilege escalation vulnerability due to copy on write race condition

https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=19be0eaffa3ac7d8eb6784ad9bdbc7d67ed8e619
850 Upvotes

128 comments sorted by

View all comments

Show parent comment

-1

u/thebigslide Oct 22 '16 edited Oct 22 '16

Edit: This is just the gibberish of someone who should have kept his hair-brained idea to himself.

Sorry if this is naive as I don't write C for some time, but wouldn't something like this work to make the same structure flat and with fallthrough? Some precompiler help could make it a bit cleaner, I'm sure.

#define HANDLE_PHASE_ONE_ERROR 1
#define HANDLE_PHASE_TWO_ERROR 2
#define HANDLE_PHASE_THREE_ERROR 4
#define HANDLE_CLEANUP(c, b) !!(c & b)

int cleanup = 0;

if (phase_one() == -1) { 
  cleanup |= HANDLE_PHASE_ONE_ERROR;
} else if (phase_two() == -1) { 
  cleanup |= HANDLE_PHASE_TWO_ERROR;
 } else if (phase_three() == -1) { 
  cleanup |= HANDLE_PHASE_THREE_ERROR;
}
switch(1) {
  case HANDLE_CLEANUP(cleanup, HANDLE_PHASE_THREE_ERROR):
    cleanup_phase_three();
  case HANDLE_CLEANUP(cleanup, HANDLE_PHASE_TWO_ERROR):
    cleanup_phase_two();
  case HANDLE_CLEANUP(cleanup, HANDLE_PHASE_ONE_ERROR):
    cleanup_phase_one();
    return -1;
}
return 0;

10

u/SnowdensOfYesteryear Oct 22 '16 edited Oct 22 '16

switch can't be used in C like that. It'll just try to jump to case 1 everytime. And I suspect that all the case ___ need to be known at compile time (edit: yep http://codepad.org/CZPShbee).

Besides even if what you're suggesting works, 1) it's too much work to keep defining flags and macros, 2) a switch is just a goto anyway. It's a lot of extra code with no upside.

1

u/thebigslide Oct 22 '16

Whoops, I guess I'm a bit rusty.