407ebe9
http://sourceware.org/ml/gdb-patches/2005-05/threads.html#00637
407ebe9
Proposed upstream but never committed upstream.
407ebe9
d939943
2005-06-09  Jeff Johnston  <jjohnstn@redhat.com>
d939943
d939943
        * gdb.base/gdbinit.exp: New testcase.
d939943
        * gdb.base/gdbinit.sample: Sample .gdbinit for gdbinit.exp.
d939943
d939943
2005-06-08  Daniel Jacobowitz  <dan@codesourcery.com>
d939943
            Jeff Johnston  <jjohnstn@redhat.com>
d939943
d939943
        * Makefile.in (cli-cmds.o): Update.
d939943
        * configure.in: Add check for getuid.
d939943
        * configure: Regenerated.
d939943
        * config.in: Ditto.
d939943
        * main.c (captured_main): Pass -1 to source_command when loading
d939943
        gdbinit files.
d939943
        * cli/cli-cmds.c: Include "gdb_stat.h" and <fcntl.h>.
d939943
        (source_command): Update documentation.  Check permissions if
d939943
        FROM_TTY is -1.
d939943
dd46ae6
Index: gdb-7.1.90.20100720/gdb/cli/cli-cmds.c
9231e41
===================================================================
dd46ae6
--- gdb-7.1.90.20100720.orig/gdb/cli/cli-cmds.c	2010-05-17 21:28:12.000000000 +0200
dd46ae6
+++ gdb-7.1.90.20100720/gdb/cli/cli-cmds.c	2010-07-21 20:30:30.000000000 +0200
dd46ae6
@@ -39,6 +39,7 @@
d939943
 #include "source.h"
d939943
 #include "disasm.h"
dd46ae6
 #include "tracepoint.h"
d939943
+#include "gdb_stat.h"
d939943
 
d939943
 #include "ui-out.h"
dd46ae6
 
dd46ae6
@@ -487,7 +488,7 @@ Script filename extension recognition is
dd46ae6
 
dd46ae6
 int
dd46ae6
 find_and_open_script (const char *script_file, int search_path,
dd46ae6
-		      FILE **streamp, char **full_pathp)
dd46ae6
+		      FILE **streamp, char **full_pathp, int from_tty)
dd46ae6
 {
dd46ae6
   char *file;
dd46ae6
   int fd;
dd46ae6
@@ -513,6 +514,32 @@ find_and_open_script (const char *script
dd46ae6
       return 0;
dd46ae6
     }
d939943
 
d939943
+#ifdef HAVE_GETUID
d939943
+  if (from_tty == -1)
d939943
+    {
d939943
+      struct stat statbuf;
407ebe9
+
d939943
+      if (fstat (fd, &statbuf) < 0)
d939943
+	{
dd46ae6
+	  int save_errno = errno;
dd46ae6
+
9231e41
+	  close (fd);
dd46ae6
+	  do_cleanups (old_cleanups);
dd46ae6
+	  errno = save_errno;
dd46ae6
+	  return 0;
d939943
+	}
d939943
+      if (statbuf.st_uid != getuid () || (statbuf.st_mode & S_IWOTH))
d939943
+	{
407ebe9
+	  /* FILE gets freed by do_cleanups (old_cleanups).  */
407ebe9
+	  warning (_("not using untrusted file \"%s\""), file);
9231e41
+	  close (fd);
407ebe9
+	  do_cleanups (old_cleanups);
dd46ae6
+	  errno = EPERM;
79563d6
+	  return 0;
d939943
+	}
d939943
+    }
d939943
+#endif
d939943
+
dd46ae6
   do_cleanups (old_cleanups);
dd46ae6
 
dd46ae6
   *streamp = fdopen (fd, FOPEN_RT);
dd46ae6
@@ -572,13 +599,14 @@ source_script_with_search (const char *f
dd46ae6
   if (file == NULL || *file == 0)
dd46ae6
     error (_("source command requires file name of file to source."));
79563d6
 
dd46ae6
-  if (!find_and_open_script (file, search_path, &stream, &full_path))
dd46ae6
+  if (!find_and_open_script (file, search_path, &stream, &full_path,
dd46ae6
+			     from_tty))
79563d6
     {
dd46ae6
       /* The script wasn't found, or was otherwise inaccessible.
dd46ae6
 	 If the source command was invoked interactively, throw an error.
dd46ae6
 	 Otherwise (e.g. if it was invoked by a script), silently ignore
dd46ae6
 	 the error.  */
79563d6
-      if (from_tty)
79563d6
+      if (from_tty > 0)
79563d6
 	perror_with_name (file);
79563d6
       else
dd46ae6
 	return;
dd46ae6
Index: gdb-7.1.90.20100720/gdb/testsuite/gdb.base/gdbinit.exp
9231e41
===================================================================
407ebe9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dd46ae6
+++ gdb-7.1.90.20100720/gdb/testsuite/gdb.base/gdbinit.exp	2010-07-20 22:25:40.000000000 +0200
dd46ae6
@@ -0,0 +1,91 @@
d939943
+#   Copyright 2005
d939943
+#   Free Software Foundation, Inc.
d939943
+
d939943
+# This program is free software; you can redistribute it and/or modify
d939943
+# it under the terms of the GNU General Public License as published by
d939943
+# the Free Software Foundation; either version 2 of the License, or
d939943
+# (at your option) any later version.
d939943
+# 
d939943
+# This program is distributed in the hope that it will be useful,
d939943
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
d939943
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d939943
+# GNU General Public License for more details.
d939943
+# 
d939943
+# You should have received a copy of the GNU General Public License
d939943
+# along with this program; if not, write to the Free Software
407ebe9
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
d939943
+
d939943
+# Please email any bugs, comments, and/or additions to this file to:
d939943
+# bug-gdb@prep.ai.mit.edu
d939943
+
d939943
+# This file was written by Jeff Johnston <jjohnstn@redhat.com>.
d939943
+
d939943
+# are we on a target board
d939943
+if [is_remote target] {
d939943
+    return
d939943
+}
d939943
+
d939943
+
d939943
+global verbose
d939943
+global GDB
d939943
+global GDBFLAGS
d939943
+global gdb_prompt
d939943
+global timeout
d939943
+global gdb_spawn_id;
407ebe9
+
d939943
+gdb_stop_suppressing_tests;
407ebe9
+
d939943
+verbose "Spawning $GDB -nw"
407ebe9
+
d939943
+if [info exists gdb_spawn_id] {
d939943
+    return 0;
d939943
+}
407ebe9
+
d939943
+if ![is_remote host] {
d939943
+   if { [which $GDB] == 0 } then {
d939943
+        perror "$GDB does not exist."
d939943
+        exit 1
d939943
+    }
d939943
+}
d939943
+
d939943
+set env(HOME) [pwd]
d939943
+remote_exec build "rm .gdbinit"
d939943
+remote_exec build "cp ${srcdir}/${subdir}/gdbinit.sample .gdbinit"
d939943
+remote_exec build "chmod 646 .gdbinit"
d939943
+
d939943
+set res [remote_spawn host "$GDB -nw [host_info gdb_opts]"];
d939943
+if { $res < 0 || $res == "" } {
d939943
+    perror "Spawning $GDB failed."
d939943
+    return 1;
d939943
+}
d939943
+gdb_expect 360 {
d939943
+    -re "warning: not using untrusted file.*\.gdbinit.*\[\r\n\]$gdb_prompt $" {
d939943
+        pass "untrusted .gdbinit caught."
d939943
+    }
d939943
+    -re "$gdb_prompt $"     {
d939943
+        fail "untrusted .gdbinit caught."
d939943
+    }
d939943
+    timeout {
d939943
+        fail "(timeout) untrusted .gdbinit caught."
d939943
+    }
d939943
+}
d939943
+
d939943
+remote_exec build "chmod 644 .gdbinit"
d939943
+set res [remote_spawn host "$GDB -nw [host_info gdb_opts]"];
d939943
+if { $res < 0 || $res == "" } {
d939943
+    perror "Spawning $GDB failed."
d939943
+    return 1;
d939943
+}
d939943
+gdb_expect 360 {
d939943
+    -re "warning: not using untrusted file.*\.gdbinit.*\[\r\n\]$gdb_prompt $" {
d939943
+        fail "trusted .gdbinit allowed."
d939943
+    }
d939943
+    -re "in gdbinit.*$gdb_prompt $"     {
d939943
+        pass "trusted .gdbinit allowed."
d939943
+    }
d939943
+    timeout {
d939943
+        fail "(timeout) trusted .gdbinit allowed."
d939943
+    }
d939943
+}
d939943
+
d939943
+remote_exec build "rm .gdbinit"
dd46ae6
Index: gdb-7.1.90.20100720/gdb/testsuite/gdb.base/gdbinit.sample
9231e41
===================================================================
407ebe9
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
dd46ae6
+++ gdb-7.1.90.20100720/gdb/testsuite/gdb.base/gdbinit.sample	2010-07-20 22:25:40.000000000 +0200
d939943
@@ -0,0 +1 @@
d939943
+echo "\nin gdbinit"
dd46ae6
Index: gdb-7.1.90.20100720/gdb/main.c
9231e41
===================================================================
dd46ae6
--- gdb-7.1.90.20100720.orig/gdb/main.c	2010-06-26 08:44:47.000000000 +0200
dd46ae6
+++ gdb-7.1.90.20100720/gdb/main.c	2010-07-21 20:09:03.000000000 +0200
dd46ae6
@@ -794,7 +794,7 @@ Excess command line arguments ignored. (
81783d0
      debugging or what directory you are in.  */
adbc6b8
 
81783d0
   if (home_gdbinit && !inhibit_gdbinit)
81783d0
-    catch_command_errors (source_script, home_gdbinit, 0, RETURN_MASK_ALL);
81783d0
+    catch_command_errors (source_script, home_gdbinit, -1, RETURN_MASK_ALL);
adbc6b8
 
81783d0
   /* Now perform all the actions indicated by the arguments.  */
81783d0
   if (cdarg != NULL)
dd46ae6
@@ -868,7 +868,7 @@ Can't attach to process and specify a co
81783d0
   /* Read the .gdbinit file in the current directory, *if* it isn't
81783d0
      the same as the $HOME/.gdbinit file (it should exist, also).  */
81783d0
   if (local_gdbinit && !inhibit_gdbinit)
81783d0
-    catch_command_errors (source_script, local_gdbinit, 0, RETURN_MASK_ALL);
81783d0
+    catch_command_errors (source_script, local_gdbinit, -1, RETURN_MASK_ALL);
adbc6b8
 
dd46ae6
   /* Now that all .gdbinit's have been read and all -d options have been
dd46ae6
      processed, we can read any scripts mentioned in SYMARG.
dd46ae6
Index: gdb-7.1.90.20100720/gdb/python/py-auto-load.c
dd46ae6
===================================================================
dd46ae6
--- gdb-7.1.90.20100720.orig/gdb/python/py-auto-load.c	2010-05-17 23:23:25.000000000 +0200
dd46ae6
+++ gdb-7.1.90.20100720/gdb/python/py-auto-load.c	2010-07-20 22:25:40.000000000 +0200
dd46ae6
@@ -219,7 +219,7 @@ source_section_scripts (struct objfile *
dd46ae6
 	}
dd46ae6
 
dd46ae6
       opened = find_and_open_script (file, 1 /*search_path*/,
dd46ae6
-				     &stream, &full_path);
dd46ae6
+				     &stream, &full_path, 1 /* from_tty */);
dd46ae6
 
dd46ae6
       /* If the file is not found, we still record the file in the hash table,
dd46ae6
 	 we only want to print an error message once.
dd46ae6
Index: gdb-7.1.90.20100720/gdb/cli/cli-cmds.h
dd46ae6
===================================================================
dd46ae6
--- gdb-7.1.90.20100720.orig/gdb/cli/cli-cmds.h	2010-05-03 01:52:14.000000000 +0200
dd46ae6
+++ gdb-7.1.90.20100720/gdb/cli/cli-cmds.h	2010-07-20 22:25:40.000000000 +0200
dd46ae6
@@ -126,7 +126,8 @@ extern void source_script (char *, int);
dd46ae6
 /* Exported to objfiles.c.  */
dd46ae6
 
dd46ae6
 extern int find_and_open_script (const char *file, int search_path,
dd46ae6
-				 FILE **streamp, char **full_path);
dd46ae6
+				 FILE **streamp, char **full_path,
dd46ae6
+				 int from_tty);
dd46ae6
 
dd46ae6
 /* Command tracing state.  */
dd46ae6