It's trivial !
Just look at the source code of kate and kwrite:
diff --git a/kate/main.cpp b/kwrite/main.cpp
In the first lines of code directly at the start of the "main" function, you’ll find:
/**
* Check whether we are running as root
**/
if (getuid() == 0) // uid = user id = 0 ==> means if you are user 0 (aka root)
{
std::cout << "Executing Kate as root is not possible. To edit files as root use:" << std::endl;
std::cout << "SUDO_EDITOR=kate sudoedit <file>" << std::endl;
return 0;
}
Dolphin is no different, except this happens in libkdeinit5_dolphin.so.
So running as root is trivial, all we need to do is skip the if uid=0 check.
The way we do this, is by perma-patching kate, kwrite and dolphin.
So, the first thing we do, is dumping the binary content to a text-file:
objdump -Crd /usr/bin/kate >> ~/kate.txt
objdump -Crd /usr/bin/kwrite >> ~/kwrite.txt
Now you can look at the file with gedit ~/kate.txt
, and if you search for getuid, you'll find something like:
2a985: 31 c0 xor %eax,%eax
2a987: 89 bc 24 dc 00 00 00 mov %edi,0xdc(%rsp)
2a98e: e8 ed ce ff ff callq 27880 <getuid@plt>
2a993: 85 c0 test %eax,%eax
2a995: 0f 84 e9 1e 00 00 je 2c884 <__cxa_finalize@plt+0x1f5c>
Now, on the left side, you find the memory address, after the colon (:) you'll see the binary instruction code (hex) and to the right of that, you see the disassembly of that code (aka what it means).
Now, you see, there it calls getuid, tests if that is zero and jumps into the if-statement, that is to say if the return value is equal to zero (je: jump if equal).
Now we don't want to jump into the if, so we just remove that crap there.
But just removing that crap would change addresses by 6 bytes, trashing any relative jumps in the program in the process, and as a consequence, the program. So instead you just replace the entire length of the jump statement with NOP (short for No Operation) instructions, aka 0x90 in instruction code/hex.
So you need to replace every jump byte there with 0x90 so where you previously had
0f 84 e9 1e 00 00
you will have
90 90 90 90 90 90
This can be done simply with a hex-editor.
So we install one:
sudo apt-get install wxhexeditor
Now in the hex editor, you search for 0f 84 e9 1e 00 00, and replace that with 90 90 90 90 90 90. If there's only one occurency of 0f 84 e9 1e 00 00, and there is, then this is trivially simple. Just change the bytes in hex to 90 90 90 90 90 90 and save. Finished. Kate or kwrite will now open, whether you are root or not.
if you do the same with dolphin, you'll realize objdump -Crd /usr/bin/dolphin
produces a very short disassembly.
if you run ldd /usr/bin/dolphin
, you'll see dolphin loads the shared library libkdeinit5_dolphin.so
linux-vdso.so.1 (0x00007ffc2fdf0000) libkdeinit5_dolphin.so =>
/usr/lib/x86_64-linux-gnu/libkdeinit5_dolphin.so (0x00007fb54e76c000)
So you do the objdump on libkdeinit5_dolphin.so:
objdump -Crd /usr/lib/x86_64-linux-gnu/libkdeinit5_dolphin.so >> ~/libkdeinit5_dolphin.txt
Now open the objdump: gedit ~/libkdeinit5_dolphin.txt
Search for getuid, and you'll see that one of the search results is:
41f95: 31 c0 xor %eax,%eax
41f97: 89 7c 24 5c mov %edi,0x5c(%rsp)
41f9b: e8 50 b6 ff ff callq 3d5f0 <getuid@plt>
41fa0: 85 c0 test %eax,%eax
41fa2: 0f 84 58 1a 00 00 je 43a00 <kdemain@@Base+0x1a90>
41fa8: 48 8d 84 24 a0 00 00 lea 0xa0(%rsp),%rax
This is great, you see, more crap just like in kate and kwrite.
Now open /usr/lib/x86_64-linux-gnu/libkdeinit5_dolphin.so
in your hex-editor, search for 0f 84 58 1a 00 00
and replace it with 90 90 90 90 90 90
.
Hit save, and done.
Dolphin now runs as root.
Note: Good idea to make a backup copy of the files you modify, just in case you f*** up.
Also, you could just download the source of kate, kwrite, and dolphin, remove that crap from the source code, compile and install. But since the stupid cmake system is missing some crappy crap template for some stupid crap such as icons, probably because the repo-supplied cmake is too old, this doesn't work. Too bad, would have been too simple if it just worked, wouldn't it.
But just patching the executables as I described is faster, so who cares anyway.
You see, it's not straightforward, but it's trivial.
PS:
Now, any time kate, kwrite or dolphin are updated via apt, your changes will be overwritten. You'll need to re-apply them. I'll leave automatizing the patch-process in your more than capable hands, and your programming language of joice ;)
Can this be done in pure bash ?
Also, if you want to patch vlc for the same crap, you can do it with sed:
sed -i 's/geteuid/getppid/' /usr/bin/vlc
Just put that sed-statement into a script, so you can re-apply, if you ever need to if you are offline and have no internet access.
Happy hacking - with kate, kwrite and dolphin - as root - while watching/listening to something on vlc.
PS2:
The crappy root checks went the way of the Dodo in KDE v19.04.
Who said nevolution was a bad thing.
To progress - Cheers !
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then xhost +si:localuser:root && pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true dbus-launch dolphin %U && xhost -si:localuser:root ; else pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY KDE_SESSION_VERSION=5 KDE_FULL_SESSION=true dolphin %U; fi;