public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Oleg Nesterov <oleg@redhat.com>
To: hank <pyu@redhat.com>
Cc: tj@kernel.org, akpm@linux-foundation.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/1] set wo_stat to an init value in do_wait function
Date: Wed, 2 Nov 2011 15:51:05 +0100	[thread overview]
Message-ID: <20111102145105.GA27394@redhat.com> (raw)
In-Reply-To: <4EB0FF81.90808@redhat.com>

On 11/02, hank wrote:
>
> When all of the below conditions become true:
> 1 parent fork a child
> 2 parent ignore SIGCHLD signal
> 3 parent call waitpid function
> do_wait function won't touch the wo->stat variable.

Of course it doesn't, do_wait() fails and does nothing.

The parent ignores SIGCHLD. In this case waitpid(&status) acts as
if there are no children. Except it sleeps.

IOW,

> int main(int argc, char *argv[])
> {
>         int pid, child;
>         int status;
>         int *p;
> 
>         signal(SIGCHLD, SIG_IGN);
> 
>         child = fork();
>         if (child == 0) {
>                 sleep(1);
>                 exit(0);
>         } else if (child < 0) {
>                 perror("fork");
>                 exit(1);
>         } else {
>                 status = 0xa5a5;
>                 p = &status;
>                 printf("status addr: %p\n", p);
>                 pid = waitpid(-1, &status, WUNTRACED);
>                 printf("pid=%d status=0x%x\n", pid, status);
>                 exit(0);
>         }
>         return 0;
> }
> ========================================================
>
> After run this program, we can see the value of status is still
> 0xa5a5,so kernel do not touch this value.

Sure, this is correct.

> It may be dangerous. Because lots of programs such as 'su' don't set
> an init value for the variable 'status' when it call waitpid function,
> and after the waitpid function return, the program may check the value
> of 'status' to see the state of child.

Then this program is buggy. Once again, waitpid() fails. The program
shouldn't look at status at all.

Oleg.


      reply	other threads:[~2011-11-02 14:55 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-02  8:29 [PATCH 1/1] set wo_stat to an init value in do_wait function hank
2011-11-02 14:51 ` Oleg Nesterov [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=20111102145105.GA27394@redhat.com \
    --to=oleg@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pyu@redhat.com \
    --cc=tj@kernel.org \
    /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