Berkeley CSUA MOTD:Entry 46925
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/24 [General] UID:1000 Activity:popular
5/24    

2007/6/12-14 [Computer/SW/Unix] UID:46925 Activity:high
6/12    Inside of a C++ program, I do a "ps | grep usename" for logging
        purposes.  where username = getenv("USER");  Doing this directly is a
        gigantic security hole because someone could set $USER to some command
        line and execute arbitrary code.  What's the best way to make this
        safe?  Is there some standard way to check the input in a case like
        this?
        \_ How about "ps | grep \"username\""?
        \_ man getuid, man getpwuid
        \_ How's your motd logger going?
           \_ I hope OP isn't doing this for a motd logger.  There are much
              easier ways than writing C++.
        \_ Some versions of ps support a -U flag (or similar) that lets you
           pass in the username OR userid. Safest way would be to (1) take the
           username and translate it to the uid via getpwuid, getpwnam,
           &c.; (2) exec ps (w/ the full path) and that uid; and (3) read the
           output in C++. Any otherway is not 100% safe.
           output in C++. Any other way is not 100% safe.
           If your version of ps does not support user filtering, you should
           exec ps (w/ the full path) and read/filter the output yourself.
           Whatever you do, don't use system() and if you are running as root,
           please drop privileges before calling exec().
           \_ Not running as root.  system() is bad, huh?
              \_ Yes. system invokes a shell for you (in some cases csh). And
                 please use a full path, last thing you want is to be running
                 a PFY's hax0r'ed version of ps.
              \_ system() is basically  a wrapper around '/bin/sh -c $command'
                 with all the vulnerabilities and performance hit you get from
                 spawning the /bin/sh -c and what the shell might do with
                 $command.  You're generally safer with fork && exec(command)
                 though then you have to deal with $PATH and massaging the
                 arguments.
                 \_ you should also read up on IFS.
                 \_ you should also read up on IFS. --psb
                    \_ What is IFS?
                       \_ IFS stands for Internal Field Separator, it is
                          what the shell uses to separate elements of the
                          various *PATH variables, among other things.
                          \_ and at the heart of many old skool attacks
                             such as /usr/lib/ex3.7preserve and other
                             insecure popen() problems.
                             insecure popen() problems. --psb
                 \_ Ok, but the command line I'm passing to system is pretty
                    complex.  I don't care much about the performance, since
                    the logging is pretty rare.  But I used "ps | grep $USER |
                    | sort | head" to get only the results I wanted.  Seems
                    like fork exec would in this case would be hard. -op
                    \_ yes it would be.  secure code is hard. insecure
                       code is easy.
                    \_ One possible sol'n would be to implement your filter
                       as a one line perl command and then send the output of
                       ps to that perl command. You would reduce the problems
                       to two fork/execs and would increase your security.
                       But the safest way is still to do as much as you can
                       in C and not in the shell via system().
                       BTW, why do use use $USER from the environment? Can't
                       you read it in using a CLI option or use the current
                       user id via getuid() or geteuid()?
                       \_ Actually, because I didn't know about getuid().
                          \_ I'm not sure what exactly you are trying to do,
                             but I think you can do all of it w/o system()
                             and not too much work in c++. Based on the above,
                             it seems like you could read the output of ps -U
                             [uid] (or equivalent) into a STL string vector,
                             sort the results and take the top 10.
2025/05/24 [General] UID:1000 Activity:popular
5/24    

You may also be interested in these entries...
2012/8/30-11/7 [Computer/SW/Apps, Computer/SW/Unix] UID:54470 Activity:nil
8/30    Is wall just dead? The wallall command dies for me, muttering
        something about /var/wall/ttys not existing.
        \_ its seen a great drop in usage, though it seems mostly functional.
            -ERic
        \_ Couldn't open wall log!: Bad file descriptor
           Could not open wall subscription directory /var/wall/ttys: No such file or directory
	...
2012/9/20-11/7 [Computer/SW/Unix, Finance/Investment] UID:54482 Activity:nil
9/20    How do I change my shell? chsh says "Cannot change ID to root."
        \_ /usr/bin/chsh does not have the SUID permission set. Without
           being set, it does not successfully change a user's shell.
           Typical newbie sys admin (on soda)
           \_ Actually, it does: -rwsr-xr-x 1 root root 37552 Feb 15  2011 /usr/bin/chsh
	...
2012/9/24-11/7 [Computer/SW/Languages, Computer/SW/Unix] UID:54484 Activity:nil
9/24    How come changing my shell using ldapmodify (chsh doesn't work) doesn't
        work either? ldapsearch and getent show the new shell but I still get
        the old shell on login.
        \_ Scratch that, it magically took my new shell now. WTF?
           \_ probably nscd(8)
	...
2012/4/27-6/4 [Computer/SW/Languages/Misc, Computer/SW/Unix] UID:54372 Activity:nil
4/27    I wrote a little shell script to collect iostat data:
        #!/bin/bash
        DATE=`date +%m%d`
        DATADIR=/var/tmp/user
        OUTPUTFILE=$DATADIR/$DATE.out
        while true
	...
2011/11/20-2012/2/6 [Computer/Companies/Apple, Computer/SW/Unix] UID:54237 Activity:nil
11/20   Are there tools that can justify a chunk of plain ASCII text by
        replacing words with words of similar meaning and inserting/removing
        commas into the text?  I received a 40-line plain text mail where
        all the lines are justified on left and right.  Every word and comma
        is followed by only one space, and every period is followed by two
        spaces.  The guy is my kid's karate instructor which I don't think is
	...
2011/10/26-12/6 [Computer/SW/Unix] UID:54202 Activity:nil
10/24  What's an easy way to see if say column 3 of a file matches a list of
       expressions in a file? Basically I want to combine "grep -f <file>"
       to store the patterns and awk's $3 ~ /(AAA|BBB|CCC)/ ... I realize
       I can do this with "egrep -f " and use regexp instead of strings, but
       was wondering if there was some magic way to do this.
       \_ UNIX has no magic. Make a shell script to produce the ask or egrep
	...