Archive for Uncategorized

The Linux and Unix Menagerie: Securing SUID Programs Using A Simple C Wrapper

This is an issue that comes up almost constantly, given the very nature of the Linux and Unix security model and the environments in which most of us admins work. More often than not, application users on machines will “need” to run scripts that require root privilege to function correctly. One such example would be the “ping” command. Although this seems like a harmless, and openly available, network troubleshooting tool, the only reason regular users can run it is because it’s SUID-root. This, as simply as possible, means that the command runs as the user “root” no matter who actually invokes it. The setuid bit (the “s” in -r-sr-xr-x) is a permission that indicates that the given command will run as the userid that owns it. root owns the ping command; therefore, when users run ping, they’re running it as the user root.

Now, the ping command has been around for quite a while and, as with almost all setuid programs, it’s been the subject of many security compromises and exploits over the years. In general, because of the fact that “ping” is such an integral part of the OS, you don’t need to worry about wrapping it (or other programs like it) in order to protect yourself against harm (Your vendor - or the development community - should be trying their hardest to do that for you :)

Instances do exist where regular users require that an uncommon command be run on a frequent basis, in order for them to do their jobs. That program (we’ll just call it “PROGRAM” for no particular reason ;) needs to be run as another user for it to function correctly and it has to be run frequently enough that it becomes an inconvenience to “not” allow the users to run the command themselves. SUID (or setuid) wrapper scripts can be most effectively used in these sorts of situations.

A wrapper script/program, is (for all intents and purposes) just another layer of protection for the admins, users and operating system. If created properly and used judiciously, they can help minimize the risk associated with allowing regular users to run commands as userid’s other than their own.

Optimally, you would want to limit SUID script/program execution to another generic user (if possible). So, for instance, if an application user needs a program to be run as the user oracle, setting them up with a shell wrapper to run that command as the oracle user shouldn’t be cause for too much concern. The greatest security risk (no matter the relative security-weight of different accounts on your system) is when you need to wrap a script or program to be run as root.

Below, I’ve put together a simple wrapper written in c ( Check out Wietse Zweitze Venema’s website, and work, for a really really really secure wrapper script ). I write the majority of my scripts in bash, ksh and Perl, but the SUID wrapper really requires that it be compiled in order to serve it’s purpose most effectively. If people can easily read your code, it’ll be easier for them to figure out ways around whatever steps you’re taking to secure your servers. I’m not saying that, just because they could read your code, they could break it; but it would certainly make it easier for them. In the other extreme circumstance, if anyone got write access to a SUID script (assuming root privilege, since almost every OS now resets the setuid bits if a setuid script is modified by a regular user), they could (easily) change it a little and stand a good chance that no one would notice that they’d created a backdoor for themselves. If you modify a compiled c binary, it probably won’t run anymore (which is the best security there is ;)

We’ll dive into the sea of “c” in a future post, since it can be complicated and is rarely necessary to know in order to administrate, or use, a system.

For the script below, just substitute the PROGRAM you want to wrap, the arguments, if any (This script assumes only one “-v” - If you have more, add them as comma separated entries just like the first, and before the NULL entry specification), and the groupid check (Comment this out if you don’t want to use it as an extra level of access checking security). We also make sure to change the real and effective uid and gid to “root” (make this any id you want) only after performing the access checks! Extra care is taken to make sure we reset to the regular user’s real and effective uid and gid even before all that.

Note also that we use the strncmp command instead of strcmp (for string comparison) to check the command line arguments. The reason we use this is that the strncmp command requires you to give it a number as it’s final argument and will not read past that many chars (I use start and stop as my only two options, and you can see that in strncmp arguments accordingly. This helps prevent a malicious user from executing a string buffer overflow which might allow them to crack your wrapper from the command line!

This c code can be compiled on gcc at least all the way back to version 2.81.x - It can be compiled very simply, like so:

gcc -o PROGRAM wrapper.c (With -o wrapper being the option (”-o“) of whatever you want to call the compiled PROGRAM and wrapper.c being the text c code below)

Enjoy!


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


#include #include #include #include #include /******************************************** * Wrapper - Secure Yourself * * * * 2007 - Mike Golvach - eggi@comcast.net * * * * Usage: COMMAND [start|stop] * * * ********************************************/ /* Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License */ /* Define global variables */ int gid; /* main(int argc, char **argv) - main process loop */ int main(int argc, char **argv) { /* Set euid and egid to actual user */ gid = getgid(); setegid(getgid()); seteuid(getuid()); /* Confirm user is in GROUP(999) group */ if ( gid != 999 ) { printf("User Not Authorized! Exiting...n"); exit(1); } /* Check argc count only at this point */ if ( argc != 2 ) { printf("Usage: COMMAND [start|stop]n"); exit(1); } /* Set uid, gid, euid and egid to root */ setegid(0); seteuid(0); setgid(0); setuid(0); /* Check argv for proper arguments and run * the corresponding script, if invoked. */ if ( strncmp(argv[1], "start", 5) == 0 ) { if (execl("/usr/local/bin/COMMAND", "COMMAND", "-v", NULL) < 0) { perror("Execl:"); } } else if ( strncmp(argv[1], "stop", 4) == 0 ) { if (execl("/usr/local/bin/COMMAND", "COMMAND", "-v", NULL) < 0) { perror("Execl:"); } } else { printf("Usage: COMMAND [start|stop]n"); exit(1); } exit(0); }



The Linux and Unix Menagerie: Securing SUID Programs Using A Simple C Wrapper.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Custom RPMs I

RPMs can be a great way to manage the packages you install on your system. Unfortunately, not everything you might want to install is available in RPM form. Perhaps you need a more recent version of a program than the one that ships with your distribution; or maybe it’s a program you wrote yourself; or perhaps it’s just something that’s very obscure. Similar dilemmas can occur with non-program packages, such as font or clip art collections.

» Continue reading “Custom RPMs I”

Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

raw device mapping in RHEL5?

The raw devices interface has been deprecated in RHEL5. The rawdevices service and /etc/sysconfig/rawdevices file no longer exist and raw devices are now configured via udev rules. However the preferred method for performing raw I/O (ie. bypassing filesystem caching) is to open EXT3/EXT2 files with the O_DIRECT flag.

» Continue reading “raw device mapping in RHEL5?”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

LD_LIBRARY_PATH workarounds

When an executable is run that relies on shared libraries not defined in the global search path, an error similar to the following is displayed:
$ curl
ld.so.1: curl: fatal: libgcc_s.so.1: open failed: No such file or directory
Killed

» Continue reading “LD_LIBRARY_PATH workarounds”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Debugging Apache web server problems

Overview

The apache web server has become the predominant web server on the Internet for it’s scalability, standards compliance and the numerous features that come bundled with the server. As more and more features are added to apache, and as web applications evolve to meet new demands, bugs will periodically surface in applications and the web server code base itself. Since application and server bugs can lead to service failures and downtime, it is important to have a good set of tools to assist with isolating and locating problems. This article provides an introduction to debugging apache web server problems, and covers several tools and techniques that can help troubleshoot problems when they arise.
Isolating problems with apache’s single process mode
» Continue reading “Debugging Apache web server problems”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Find 1 liners

Finding Max space used by Top 10 Files in MB:

============================================

find /tmp -type f | xargs ls -msa | sort -rn | awk ‘{size=$1/1024; printf(”%dMb %sn”, size,$2);}’ | head

10Mb /tmp/openldap-devel-2.3.34-0.fc7.i386.rpm,

6Mb /tmp/newtonslaw.wav

4Mb /tmp/openldap-clients-2.3.34-0.fc7.i386.rpm,

3Mb /tmp/ntfs-3g-1.516-1.fc7.i386.rpm,

2Mb /tmp/fuse-2.6.5-1.fc7.i386.rpm,

1Mb /tmp/orbit-root/bonobo-activation-server-ior,

0Mb /tmp/gconfd-root/lock/ior,

0Mb /tmp/orbit-root/bonobo-activation-register.lock,

Finding files which are more than 10 MB:

========================================

find /tmp -name “*” -size +10000k

Finding files modified 3 days ago but less than 5 days:

========================================================

find /tmp -type f -mtime +2 -mtime -5

Find files that were accessed exactly 3 days a go

==================================================

find /tmp -type f -atime 3

-atime +7: All files that were last accessed more than 7 days ago

-atime 7: All files that were last accessed exactly 7 days ago

-atime -7: All files that were last accessed less than7 days ago

To replace recursive in *.html files, with multiline support:

=============================================================

find ./dir/ -name *.html -exec perl -pi -e ‘undef $/;

s/REPLACE-THIS/WITH-THAT/s’ {} ;

Update the modification date for all files in the

current directory and below:

=================================================

find . -exec touch {} ;

Remove files in a directory that were created more

than x number of days ago (great for managing Maildir).

======================================================

find ./ -ctime +x | xargs | rm -f -r

This should delete files older than 7 days, and

print the name of each file it deletes.

=================================================

find ./ -ctime +7 -name “*.zip” -exec rm -f {} ; -ls

# If you want to be prompted before it deletes a file:

find ./ -ctime +7 -name “*.zip” -exec rm -i {} ; -ls

This will list the files with names ending in .c

and containing the word hello:

=================================================

find . -type f -name ‘*.c’ -exec grep -l hello {} /dev/null ;

Find all the files recursively that contain foo:

================================================

find . | xargs grep foo

./a:foo

./c:foo

./d:ifoo

a,c,d are the file names that contain foo
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Watch

There is a very useful but rarely used command in Linux, watch. While those of you who know about it are surely aware of all the easy works it does for you, I want to tell it to them who didn’t know about it. watch literally watches over what you intent to use it for. From your mail account to linux directory listings to interrupts - watch can watch it over for you and display the results repeatedly at your desired time interval.

» Continue reading “Watch”
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Global command in VIM

Well you guessed it right, the “global” command (or :g in command mode) will let you select lines that match a particular pattern globally in the file and lets you operate on the selected text. The :v operates similar to grep -v which selects all texts which do not match the pattern.

via Global command in VIM « Inside Out.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Setting Up Ubuntu to Share Video, Music, Pictures with Your PlayStation 3 (PS3) - Blog-Foo

I found this great tutorial for configuring your Ubuntu desktop to share your media (such as you’re movies, TV episodes, music etc etc) over you’re home network (wired and wireless) to allow access from your PlayStation 3 (PS3). I have to say it’s quite handy, it saves having to burn multiple DVD’s every time you want to watch videos or listen to music on your PS3.

Setting Up Ubuntu to Share Video, Music, Pictures with Your PlayStation 3 (PS3) - Blog-Foo.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment

Using rsync with no command like [linux] |

For those (like me) who are interested on using rsync but are command line inexperienced, finding a GUI is a lifesaver.

grsyncGrsync can make sure you use rsync without the terminal.

via Using rsync with no command like [linux] |.
Blogmarks BlogMemes BlogLines del.icio.us de.lirio.us Digg Facebook Google Google Reader LinkaGoGo Ask.com MyStuff Ask.com Yahoo! MyWeb Netscape Sphere StumbleUpon Plugin by Dichev.com

Leave a Comment