Openwall GNU/*/Linux - a small security-enhanced Linux distro for servers
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 25 Jun 2020 13:21:05 +0000
From: Stefan Ciotec <Stefan.Ciotec@...a.com>
To: "'musl@...ts.openwall.com'" <musl@...ts.openwall.com>
CC: Vasile Iliescu <Vasile.Iliescu@...a.com>
Subject: errno not set to EBADF when reading from invalid descriptor

Hi,

We are using MUSL C-library 1.1.22.
According to the POSIX standard, EOF should be returned and errno should be set to EBADF for the read group of functions (i.e. fgetc(), getc(), getc_unlocked()) when attempting to read from a stream with an invalid file descriptor open for reading.
However, in our tests with MUSL, we discovered that EOF is returned, but errno is not set to EBADF (it's 0 instead), for the following code:

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>

int main() {
   FILE *f = fopen("/tmp/getc_unlocked", "w");
   char c;
   /* Negative smoke testing. */
   flockfile(f);
   c = getc_unlocked(f);
   printf("c:%#x errno:%d\n", c, errno);
   funlockfile(f);
   fclose(f);
   remove("/tmp/getc_unlocked");
   return 0;
}

I tested the same code with Linux glibc and errno is set appropriately to EBADF.

The issue seems to be in the following portion of the MUSL library:
int __toread(FILE *f)
{
   f->mode |= f->mode-1;
   if (f->wpos != f->wbase) f->write(f, 0, 0);
   f->wpos = f->wbase = f->wend = 0;
   if (f->flags & F_NORD) {
      f->flags |= F_ERR;
      return EOF; // Here errno should be set to EBADF before returning
   }
   f->rpos = f->rend = f->buf + f->buf_size;
   return (f->flags & F_EO
}

Best regards,

Stefan Ciotec
Software Engineer
OS Business Unit
Email
Stefan.Ciotec@...a.com
Phone
+40764758162

Enea
319 Splaiul Independentei
OB403A District 6
Bucharest 060044, ROMANIA

[https://www.enea.com/globalassets/images/enea-email-signature.png]<http://www.enea.com/>



This message, including attachments, is CONFIDENTIAL. It may also be privileged or otherwise protected by law. If you received this email by mistake please let us know by reply and then delete it from your system; you should not copy it or disclose its contents to anyone. All messages sent to and from Enea may be monitored to ensure compliance with internal policies and to protect our business. Emails are not secure and cannot be guaranteed to be error free as they can be intercepted, a mended, lost or destroyed, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message, which arise as a result of email transmission. Anyone who communicates with us by email accepts these risks.

Content of type "text/html" skipped

Powered by blists - more mailing lists

Your e-mail address:

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.