public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: cygwin@cygwin.com, pnbrown@llnl.gov
Subject: Re: system mkdir
Date: Fri, 02 Dec 2011 00:12:00 -0000	[thread overview]
Message-ID: <4ED817DE.5030003@redhat.com> (raw)
In-Reply-To: <loom.20111202T003035-848@post.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 1690 bytes --]

On 12/01/2011 04:32 PM, Peter Brown wrote:
>>> Yes.  Use the mkdir() function.  That is, in fact, very similar to how it
>>> should be done in Visual Studio too.

I still say using mkdir() is the right solution - use a simple function,
rather than a call to system().

> This seems to work for me.
> 
> int MySystem(char *s)
> {
>   int answer;
>   char syscmd[2048];
>   sprintf(syscmd,"cmd /c bash -c 'umask 0077;%s'",s);
>   answer = system( syscmd );
>   return answer;

Ouch. A thousand times NOOOOO. DON'T DO THAT.  You just made a HUGE
security hole.  All I have to do is call:

MySystem("rm -rf /");

and I've nuked your system.  Or, I could call MySystem(<string longer
than 2048 bytes>) and stack overflow you.  Or any other number of flaws.

And even if those weren't issues, you ruined my quoting.  I should be
able to do system("echo 'a  b'") and get "a  b" on stdout, but the way
you butchered quoting, my single quotes terminate the argument to cmd,
and completely hose things.

You DON'T want to use system() unless absolutely necessary, and then
only with proper quoting.  And you don't want to use sprintf on
arbitrary input; stick to snprintf, or to asprintf.  And seriously -
calling cmd /c just to call bash just to call commands that will be
interpreted by bash, when you could have just used commands interpreted
by bash to begin with?  If system() isn't calling cygwin's /bin/sh, then
you aren't using cygwin properly.  Don't make it worse by wrapping
system() to go through another two layers of quoting nightmares.

-- 
Eric Blake   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]

      reply	other threads:[~2011-12-02  0:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAKfLWNo3MTYWNZj0G-giFSe8PkBsP+0P1J+C9u-nrvo0JfrZnw@mail.gmail.com>
2011-11-28 20:34 ` Fitzy
2011-11-28 20:50   ` Christopher Faylor
2011-11-29  0:06     ` Eric Blake
2011-12-01 23:32       ` Peter Brown
2011-12-02  0:12         ` Eric Blake [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4ED817DE.5030003@redhat.com \
    --to=eblake@redhat.com \
    --cc=cygwin@cygwin.com \
    --cc=pnbrown@llnl.gov \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).