Qt: fix use-after-free on error while saving exported packets
[metze/wireshark/wip.git] / sharkd.c
index d2be886e307073683ccf8b52fa1a8b3a211c9c99..0b61074c8a8909a46e3944a9c429666d5ab212e1 100644 (file)
--- a/sharkd.c
+++ b/sharkd.c
@@ -6,7 +6,7 @@
  * By Gerald Combs <gerald@wireshark.org>
  * Copyright 1998 Gerald Combs
  *
- * SPDX-License-Identifier: GPL-2.0+
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include <config.h>
@@ -52,6 +52,7 @@
 #include "epan/register.h"
 #include <epan/epan_dissect.h>
 #include <epan/tap.h>
+#include <epan/uat-int.h>
 
 #include <codecs/codecs.h>
 
@@ -124,7 +125,7 @@ main(int argc, char *argv[])
   /*
    * Attempt to get the pathname of the executable file.
    */
-  init_progfile_dir_error = init_progfile_dir(argv[0], main);
+  init_progfile_dir_error = init_progfile_dir(argv[0]);
   if (init_progfile_dir_error != NULL) {
     fprintf(stderr, "sharkd: Can't get pathname of sharkd program: %s.\n",
             init_progfile_dir_error);
@@ -161,21 +162,7 @@ main(int argc, char *argv[])
   timestamp_set_precision(TS_PREC_AUTO);
   timestamp_set_seconds_type(TS_SECONDS_DEFAULT);
 
-  wtap_init();
-
-#ifdef HAVE_PLUGINS
-  /* Register all the plugin types we have. */
-  epan_register_plugin_types(); /* Types known to libwireshark */
-
-  /* Scan for plugins.  This does *not* call their registration routines;
-     that's done later. */
-  scan_plugins(REPORT_LOAD_FAILURE);
-
-  /* Register all libwiretap plugin modules. */
-  register_all_wiretap_modules();
-#endif
-
-  register_all_codecs();
+  wtap_init(TRUE);
 
   /* Register all dissectors; we must do this before checking for the
      "-G" flag, as the "-G" flag dumps information registered by the
@@ -187,6 +174,8 @@ main(int argc, char *argv[])
     goto clean_exit;
   }
 
+  codecs_init();
+
   /* Load libwireshark settings from the current profile. */
   prefs_p = epan_load_settings();
 
@@ -207,15 +196,19 @@ main(int argc, char *argv[])
   /* Build the column format array */
   build_column_format_array(&cfile.cinfo, prefs_p->num_cols, TRUE);
 
+#ifdef HAVE_MAXMINDDB
+  /* mmdbresolve is started from mmdb_resolve_start(), which is called from epan_load_settings via: read_prefs -> (...) uat_load_all -> maxmind_db_post_update_cb.
+   * Need to stop it, otherwise all sharkd will have same mmdbresolve process, including pipe descriptors to read and write. */
+  uat_clear(uat_get_table_by_name("MaxMind Database Paths"));
+#endif
+
   ret = sharkd_loop();
 clean_exit:
   col_cleanup(&cfile.cinfo);
   free_filter_lists();
+  codecs_cleanup();
   wtap_cleanup();
   free_progdirs();
-#ifdef HAVE_PLUGINS
-  plugins_cleanup();
-#endif
   return ret;
 }
 
@@ -255,23 +248,19 @@ sharkd_epan_new(capture_file *cf)
 
 static gboolean
 process_packet(capture_file *cf, epan_dissect_t *edt,
-               gint64 offset, struct wtap_pkthdr *whdr,
-               const guchar *pd)
+               gint64 offset, wtap_rec *rec, const guchar *pd)
 {
   frame_data     fdlocal;
-  guint32        framenum;
   gboolean       passed;
 
-  /* The frame number of this packet is one more than the count of
-     frames in this packet. */
-  framenum = cf->count + 1;
-
   /* If we're not running a display filter and we're not printing any
      packet information, we don't need to do a dissection. This means
      that all packets can be marked as 'passed'. */
   passed = TRUE;
 
-  frame_data_init(&fdlocal, framenum, whdr, offset, cum_bytes);
+  /* The frame number of this packet, if we add it to the set of frames,
+     would be one more than the count of frames in the file so far. */
+  frame_data_init(&fdlocal, cf->count + 1, rec, offset, cum_bytes);
 
   /* If we're going to print packet information, or we're going to
      run a read filter, or display filter, or we're going to process taps, set up to
@@ -301,7 +290,9 @@ process_packet(capture_file *cf, epan_dissect_t *edt,
       cf->provider.ref = &ref_frame;
     }
 
-    epan_dissect_run(edt, cf->cd_t, whdr, frame_tvbuff_new(&fdlocal, pd), &fdlocal, NULL);
+    epan_dissect_run(edt, cf->cd_t, rec,
+                     frame_tvbuff_new(&cf->provider, &fdlocal, pd),
+                     &fdlocal, NULL);
 
     /* Run the read filter if we have one. */
     if (cf->rfcode)
@@ -373,8 +364,8 @@ load_cap_file(capture_file *cf, int max_packet_count, gint64 max_byte_count)
     }
 
     while (wtap_read(cf->provider.wth, &err, &err_info, &data_offset)) {
-      if (process_packet(cf, edt, data_offset, wtap_phdr(cf->provider.wth),
-                         wtap_buf_ptr(cf->provider.wth))) {
+      if (process_packet(cf, edt, data_offset, wtap_get_rec(cf->provider.wth),
+                         wtap_get_buf_ptr(cf->provider.wth))) {
         /* Stop reading if we have the maximum number of packets;
          * When the -c option has not been used, max_packet_count
          * starts at 0, which practically means, never stop reading.
@@ -535,14 +526,14 @@ sharkd_get_frame(guint32 framenum)
 }
 
 int
-sharkd_dissect_request(unsigned int framenum, void (*cb)(epan_dissect_t *, proto_tree *, struct epan_column_info *, const GSList *, void *), int dissect_bytes, int dissect_columns, int dissect_tree, void *data)
+sharkd_dissect_request(guint32 framenum, guint32 frame_ref_num, guint32 prev_dis_num, sharkd_dissect_func_t cb, int dissect_bytes, int dissect_columns, int dissect_tree, void *data)
 {
   frame_data *fdata;
   column_info *cinfo = (dissect_columns) ? &cfile.cinfo : NULL;
   epan_dissect_t edt;
   gboolean create_proto_tree;
-  struct wtap_pkthdr phdr; /* Packet header */
-  Buffer buf; /* Packet data */
+  wtap_rec rec; /* Record metadata */
+  Buffer buf;   /* Record data */
 
   int err;
   char *err_info = NULL;
@@ -551,10 +542,10 @@ sharkd_dissect_request(unsigned int framenum, void (*cb)(epan_dissect_t *, proto
   if (fdata == NULL)
     return -1;
 
-  wtap_phdr_init(&phdr);
+  wtap_rec_init(&rec);
   ws_buffer_init(&buf, 1500);
 
-  if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &phdr, &buf, &err, &err_info)) {
+  if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &rec, &buf, &err, &err_info)) {
     ws_buffer_free(&buf);
     return -1; /* error reading the record */
   }
@@ -569,7 +560,12 @@ sharkd_dissect_request(unsigned int framenum, void (*cb)(epan_dissect_t *, proto
    * XXX - need to catch an OutOfMemoryError exception and
    * attempt to recover from it.
    */
-  epan_dissect_run(&edt, cfile.cd_t, &phdr, frame_tvbuff_new_buffer(fdata, &buf), fdata, cinfo);
+  fdata->flags.ref_time = (framenum == frame_ref_num);
+  fdata->frame_ref_num = frame_ref_num;
+  fdata->prev_dis_num = prev_dis_num;
+  epan_dissect_run(&edt, cfile.cd_t, &rec,
+                   frame_tvbuff_new_buffer(&cfile.provider, fdata, &buf),
+                   fdata, cinfo);
 
   if (cinfo) {
     /* "Stringify" non frame_data vals */
@@ -579,27 +575,27 @@ sharkd_dissect_request(unsigned int framenum, void (*cb)(epan_dissect_t *, proto
   cb(&edt, dissect_tree ? edt.tree : NULL, cinfo, dissect_bytes ? edt.pi.data_src : NULL, data);
 
   epan_dissect_cleanup(&edt);
-  wtap_phdr_cleanup(&phdr);
+  wtap_rec_cleanup(&rec);
   ws_buffer_free(&buf);
   return 0;
 }
 
 /* based on packet_list_dissect_and_cache_record */
 int
-sharkd_dissect_columns(frame_data *fdata, column_info *cinfo, gboolean dissect_color)
+sharkd_dissect_columns(frame_data *fdata, guint32 frame_ref_num, guint32 prev_dis_num, column_info *cinfo, gboolean dissect_color)
 {
   epan_dissect_t edt;
   gboolean create_proto_tree;
-  struct wtap_pkthdr phdr; /* Packet header */
-  Buffer buf; /* Packet data */
+  wtap_rec rec; /* Record metadata */
+  Buffer buf;   /* Record data */
 
   int err;
   char *err_info = NULL;
 
-  wtap_phdr_init(&phdr);
+  wtap_rec_init(&rec);
   ws_buffer_init(&buf, 1500);
 
-  if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &phdr, &buf, &err, &err_info)) {
+  if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &rec, &buf, &err, &err_info)) {
     col_fill_in_error(cinfo, fdata, FALSE, FALSE /* fill_fd_columns */);
     ws_buffer_free(&buf);
     return -1; /* error reading the record */
@@ -621,7 +617,12 @@ sharkd_dissect_columns(frame_data *fdata, column_info *cinfo, gboolean dissect_c
    * XXX - need to catch an OutOfMemoryError exception and
    * attempt to recover from it.
    */
-  epan_dissect_run(&edt, cfile.cd_t, &phdr, frame_tvbuff_new_buffer(fdata, &buf), fdata, cinfo);
+  fdata->flags.ref_time = (fdata->num == frame_ref_num);
+  fdata->frame_ref_num = frame_ref_num;
+  fdata->prev_dis_num = prev_dis_num;
+  epan_dissect_run(&edt, cfile.cd_t, &rec,
+                   frame_tvbuff_new_buffer(&cfile.provider, fdata, &buf),
+                   fdata, cinfo);
 
   if (cinfo) {
     /* "Stringify" non frame_data vals */
@@ -629,7 +630,7 @@ sharkd_dissect_columns(frame_data *fdata, column_info *cinfo, gboolean dissect_c
   }
 
   epan_dissect_cleanup(&edt);
-  wtap_phdr_cleanup(&phdr);
+  wtap_rec_cleanup(&rec);
   ws_buffer_free(&buf);
   return 0;
 }
@@ -640,7 +641,7 @@ sharkd_retap(void)
   guint32          framenum;
   frame_data      *fdata;
   Buffer           buf;
-  struct wtap_pkthdr phdr;
+  wtap_rec         rec;
   int err;
   char *err_info = NULL;
 
@@ -666,7 +667,7 @@ sharkd_retap(void)
   create_proto_tree =
     (have_filtering_tap_listeners() || (tap_flags & TL_REQUIRES_PROTO_TREE));
 
-  wtap_phdr_init(&phdr);
+  wtap_rec_init(&rec);
   ws_buffer_init(&buf, 1500);
   epan_dissect_init(&edt, cfile.epan, create_proto_tree, FALSE);
 
@@ -675,14 +676,19 @@ sharkd_retap(void)
   for (framenum = 1; framenum <= cfile.count; framenum++) {
     fdata = sharkd_get_frame(framenum);
 
-    if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &phdr, &buf, &err, &err_info))
+    if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &rec, &buf, &err, &err_info))
       break;
 
-    epan_dissect_run_with_taps(&edt, cfile.cd_t, &phdr, frame_tvbuff_new(fdata, ws_buffer_start_ptr(&buf)), fdata, cinfo);
+    fdata->flags.ref_time = FALSE;
+    fdata->frame_ref_num = (framenum != 1) ? 1 : 0;
+    fdata->prev_dis_num = framenum - 1;
+    epan_dissect_run_with_taps(&edt, cfile.cd_t, &rec,
+                               frame_tvbuff_new(&cfile.provider, fdata, ws_buffer_start_ptr(&buf)),
+                               fdata, cinfo);
     epan_dissect_reset(&edt);
   }
 
-  wtap_phdr_cleanup(&phdr);
+  wtap_rec_cleanup(&rec);
   ws_buffer_free(&buf);
   epan_dissect_cleanup(&edt);
 
@@ -696,10 +702,10 @@ sharkd_filter(const char *dftext, guint8 **result)
 {
   dfilter_t  *dfcode = NULL;
 
-  guint32 framenum;
+  guint32 framenum, prev_dis_num = 0;
   guint32 frames_count;
   Buffer buf;
-  struct wtap_pkthdr phdr;
+  wtap_rec rec;
   int err;
   char *err_info = NULL;
 
@@ -715,7 +721,7 @@ sharkd_filter(const char *dftext, guint8 **result)
 
   frames_count = cfile.count;
 
-  wtap_phdr_init(&phdr);
+  wtap_rec_init(&rec);
   ws_buffer_init(&buf, 1500);
   epan_dissect_init(&edt, cfile.epan, TRUE, FALSE);
 
@@ -730,16 +736,23 @@ sharkd_filter(const char *dftext, guint8 **result)
       passed_bits = 0;
     }
 
-    if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &phdr, &buf, &err, &err_info))
+    if (!wtap_seek_read(cfile.provider.wth, fdata->file_off, &rec, &buf, &err, &err_info))
       break;
 
     /* frame_data_set_before_dissect */
     epan_dissect_prime_with_dfilter(&edt, dfcode);
 
-    epan_dissect_run(&edt, cfile.cd_t, &phdr, frame_tvbuff_new_buffer(fdata, &buf), fdata, NULL);
+    fdata->flags.ref_time = FALSE;
+    fdata->frame_ref_num = (framenum != 1) ? 1 : 0;
+    fdata->prev_dis_num = prev_dis_num;
+    epan_dissect_run(&edt, cfile.cd_t, &rec,
+                     frame_tvbuff_new_buffer(&cfile.provider, fdata, &buf),
+                     fdata, NULL);
 
-    if (dfilter_apply_edt(dfcode, &edt))
+    if (dfilter_apply_edt(dfcode, &edt)) {
       passed_bits |= (1 << (framenum % 8));
+      prev_dis_num = framenum;
+    }
 
     /* if passed or ref -> frame_data_set_after_dissect */
 
@@ -750,7 +763,7 @@ sharkd_filter(const char *dftext, guint8 **result)
       framenum--;
   result_bits[framenum / 8] = passed_bits;
 
-  wtap_phdr_cleanup(&phdr);
+  wtap_rec_cleanup(&rec);
   ws_buffer_free(&buf);
   epan_dissect_cleanup(&edt);