Bug 19089 - Environment variable TMP may yield gcc: abort with internal error
Summary: Environment variable TMP may yield gcc: abort with internal error
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: other (show other bugs)
Version: 3.3.1
: P2 normal
Target Milestone: 12.0
Assignee: Andrew Pinski
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: ice-on-invalid-code, patch
Depends on:
Blocks:
 
Reported: 2004-12-20 12:44 UTC by M. Oliver Möller
Modified: 2022-11-28 22:59 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-07-02 01:48:30


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description M. Oliver Möller 2004-12-20 12:44:26 UTC
When a (bash) environment variable TMP is set to a file with execution
rights 0755, gcc aborts with an internal error. E.g.:

# -- bash shell --------------------------------------------------------------
$ uname -a
CYGWIN_NT-5.1 DF8JLP0J 1.5.5(0.94/3/2) 2003-09-20 16:31 i686 unknown unknown Cygwin
$ gcc --version 
gcc (GCC) 3.3.1 (cygming special)
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat a.c

#include <stdlib.h>
#include <stdio.h>
int main(int argc , char *argv[]){
  printf("Ok.\n");
  return 0;
}
$ unset TMP
$ gcc -save-temps a.c
$ touch /tmp/file.txt
$ export TMP=/tmp/file.txt
$ gcc -save-temps a.c
$ chmod u+x /tmp/file.txt
$ gcc -save-temps a.c
$ chmod 0755 /tmp/file.txt 
$ gcc -save-temps a.c
gcc: Internal error: Aborted (program collect2)
Please submit a full bug report.
See <URL:http://gcc.gnu.org/bugs.html> for instructions.
## ----------------------------------------------------------

The MAN page does not note a dependency to TMP (only TMPDIR) ist listed.
Comment 1 Andrew Pinski 2004-12-20 13:01:28 UTC
In a way this is not a bug:
  fd = mkstemps (temp_filename, suffix_len);
  /* If mkstemps failed, then something bad is happening.  Maybe we should
     issue a message about a possible security attack in progress?  */
  if (fd == -1)
    abort ();
  /* Similarly if we can not close the file.  */
  if (close (fd))
    abort ();
Comment 2 Andrew Pinski 2005-07-02 01:48:30 UTC
Confirmed.
Comment 3 Andrew Pinski 2021-11-28 02:02:56 UTC
I can't reproduce the crash any more but it should not be hard to fix.

The problem is in make-temp-file.c where we do:
static inline const char *
try_dir (const char *dir, const char *base)
{
  if (base != 0)
    return base;
  if (dir != 0
      && access (dir, R_OK | W_OK | X_OK) == 0)
    return dir;
  return 0;
}

But we don't check to see if the dir is actually a directory.
Comment 4 Andrew Pinski 2021-11-28 02:16:53 UTC
Something like after the access check has passed:

#ifdef S_ISDIR
struct stat s;
if (stat(dir, &s) <0)
  return NULL;
if (!S_ISDIR (s.st_mode))
  return NULL;
#endif
Comment 5 Andrew Pinski 2021-11-28 02:52:51 UTC
Patch submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2021-November/585591.html

The only issue is hosts where S_ISDIR is not defined (I don't think there is any left but it least it will just fail like it used to).
Comment 6 GCC Commits 2021-11-29 00:43:21 UTC
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:68332ab7ec58a89660db82569c5f4c2251d59741

commit r12-5568-g68332ab7ec58a89660db82569c5f4c2251d59741
Author: Andrew Pinski <apinski@marvell.com>
Date:   Sat Nov 27 18:16:50 2021 -0800

    Fix PR 19089: Environment variable TMP may yield gcc: abort
    
    Even though I cannot reproduce the ICE any more, this is still
    a bug. We check already to see if we can access the directory
    but never check to see if the path is actually a directory.
    
    This adds the check and now we reject the file as not usable
    as a tmp directory.
    
    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
    
    libiberty/ChangeLog:
    
            * make-temp-file.c (try_dir): Check to see if the dir
            is actually a directory.
Comment 7 Andrew Pinski 2021-11-29 00:44:25 UTC
Fixed. Sorry it took so long in fixing this issue.