Search code examples
c++posix

Are system() calls evil?


I am designing an C++ app that, among other things, executes a few scripts every now and then. The app should be efficient and preferably platform independent.

The issue is, however: is there a reason one shouldn't use system() call for launching scripts and use, for example, POSIX facilities instead? The discussion on the matter that I've seen so far usually boils down to:

  1. system() is less flexible. (Fine with me)
  2. It offers no control of the command being executed. (Fine with me, I just need a return value from the script)
  3. It is not quite platform independent. (Now, this would be a concern. I would really love to see an example where it behaves differently on different platforms)
  4. It is a security concern. (Again, this would be an issue. Can someone provide an example of a potential security problem with system()? )
  5. Any other issues?

Solution

  • (3) If you just want a counterexample, for example grep behaves differently on Solaris vs Linux vs whatever.

    (4) Your program's privileges are inherited by its spawned programs. If your application ever runs as a privileged user, all someone has to do is put their own program with the name of the thing you shell out too, and then can execute arbitrary code (this implies you should never run a program that uses system as root or setuid root).

    (5) It will probably be saner to maintain in the long run to use the posix facilities because you won't have to rely on a specific set of external scripts or binaries already existing wherever your program runs.