ctdb: Use talloc_report_str in ctdb
authorVolker Lendecke <vl@samba.org>
Sun, 16 Aug 2015 11:19:15 +0000 (13:19 +0200)
committerVolker Lendecke <vl@samba.org>
Thu, 20 Aug 2015 10:49:22 +0000 (12:49 +0200)
This fixes CID 1125620 Insecure temporary file

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ira Cooper <ira@samba.org>
ctdb/server/ctdb_control.c
ctdb/wscript
lib/util/wscript_build

index 59b7d098671ee7e6a1cf48c553a32f81a88b3e74..ee69270c784899a1fc6d52e5f7212a832551b60b 100644 (file)
@@ -24,6 +24,7 @@
 #include "../include/ctdb_private.h"
 #include "lib/util/dlinklist.h"
 #include "lib/tdb_wrap/tdb_wrap.h"
+#include "lib/util/talloc_report.h"
 
 
 struct ctdb_control_state {
@@ -40,34 +41,23 @@ struct ctdb_control_state {
  */
 int32_t ctdb_dump_memory(struct ctdb_context *ctdb, TDB_DATA *outdata)
 {
-       /* dump to a file, then send the file as a blob */
-       FILE *f;
-       long fsize;
-       f = tmpfile();
-       if (f == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n", strerror(errno)));
-               return -1;
-       }
-       talloc_report_full(NULL, f);
-       fsize = ftell(f);
-       if (fsize == -1) {
-               DEBUG(DEBUG_ERR, (__location__ " Unable to get file size - %s\n",
-                                 strerror(errno)));
-               fclose(f);
+       char *report;
+       size_t reportlen;
+
+       report = talloc_report_str(outdata, NULL);
+       if (report == NULL) {
+               DEBUG(DEBUG_ERR,
+                     (__location__ " talloc_report_str failed\n"));
                return -1;
        }
-       rewind(f);
-       outdata->dptr = talloc_size(outdata, fsize);
-       if (outdata->dptr == NULL) {
-               fclose(f);
-               CTDB_NO_MEMORY(ctdb, outdata->dptr);
-       }
-       outdata->dsize = fread(outdata->dptr, 1, fsize, f);
-       fclose(f);
-       if (outdata->dsize != fsize) {
-               DEBUG(DEBUG_ERR,(__location__ " Unable to read tmpfile\n"));
-               return -1;
+       reportlen = talloc_get_size(report);
+
+       if (reportlen > 0) {
+               reportlen -= 1; /* strip trailing zero */
        }
+
+       outdata->dptr = (uint8_t *)report;
+       outdata->dsize = reportlen;
        return 0;
 }
 
index 06f86a31916f16a7729c2cf3115d02f1262dd87f..8aa037409e077493fce0c4c7f4e7ec36b4528970 100755 (executable)
@@ -348,7 +348,7 @@ def build(bld):
                                           include/ctdb_private.h
                                           include/ctdb_protocol.h
                                           include/ctdb_typesafe_cb.h''',
-                        deps='replace popt talloc tevent tdb')
+                        deps='replace popt talloc tevent tdb talloc_report')
 
     bld.SAMBA_BINARY('ctdbd',
                      source='',
@@ -547,7 +547,7 @@ def build(bld):
     bld.SAMBA_BINARY('ctdb_takeover_tests',
                      source='tests/src/ctdb_takeover_tests.c',
                      deps='''replace popt tdb tevent talloc ctdb-system
-                             samba-util tdb-wrap''' +
+                             samba-util tdb-wrap talloc_report''' +
                           ib_deps,
                      includes='include include/internal',
                      install_path='${CTDB_TEST_LIBDIR}')
index 9663bb0fd98e679ac5f291ab9eecb14d00e7f40b..e5c1a97d15187aea412ae1564afa24c1c58f9cee 100755 (executable)
@@ -54,6 +54,13 @@ bld.SAMBA_LIBRARY('socket-blocking',
                   local_include=False,
                   private_library=True)
 
+bld.SAMBA_LIBRARY('talloc_report',
+                  source='talloc_report.c',
+                  local_include=False,
+                  public_deps='talloc',
+                  private_library=True
+                  )
+
 bld.SAMBA_SUBSYSTEM('samba-util-core',
                     source='''xfile.c data_blob.c util_file.c time.c
                               signal.c util.c idtree.c fault.c
@@ -119,13 +126,6 @@ if not bld.env.SAMBA_UTIL_CORE_ONLY:
                       private_library=True
                       )
 
-    bld.SAMBA_LIBRARY('talloc_report',
-                      source='talloc_report.c',
-                      local_include=False,
-                      public_deps='talloc',
-                      private_library=True
-                      )
-
     bld.SAMBA_LIBRARY('tevent-util',
                       source='tevent_unix.c tevent_ntstatus.c tevent_werror.c',
                       local_include=False,