Ignore:
Timestamp:
12/19/18 15:35:26 (23 months ago)
Author:
Jacob Van Walraven <jcv9@…>
Branches:
develop
Children:
630460b
Parents:
6327ce6
Message:

Instead we should still copy the filter but make sure its freed when copying another, this protects against the user freeing a filter that is in use. Issue #87

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/format_linux_common.c

    r6327ce6 r8df87c4  
    6666        unsigned int arphrd;
    6767        libtrace_dlt_t dlt;
     68        libtrace_filter_t *f;
    6869        int sock;
    6970        pcap_t *pcap;
     71
     72        /* Take a copy of the filter structure to prevent against
     73         * deletion causing the filter to no longer work */
     74        f = (libtrace_filter_t *) malloc(sizeof(libtrace_filter_t));
     75        memcpy(f, filter, sizeof(libtrace_filter_t));
     76        f->filterstring = strdup(filter->filterstring);
    7077
    7178        /* If we are passed a filter with "flag" set to zero, then we must
     
    7885         * anything (we've just copied it above).
    7986         */
    80         if (filter->flag == 0) {
     87        if (f->flag == 0) {
    8188                sock = socket(PF_INET, SOCK_STREAM, 0);
    8289                memset(&ifr, 0, sizeof(struct ifreq));
     
    94101                                FORMAT_DATA->snaplen);
    95102
    96                 if (pcap_compile(pcap, &filter->filter, filter->filterstring, 0, 0) == -1) {
     103                if (pcap_compile(pcap, &f->filter, f->filterstring, 0, 0) == -1) {
    97104                        /* Filter didn't compile, set flag to 0 so we can
    98105                         * detect this when trace_start() is called and
    99106                         * produce a useful error
    100107                         */
    101                         filter->flag = 0;
     108                        f->flag = 0;
    102109                        trace_set_err(libtrace, TRACE_ERR_INIT_FAILED,
    103110                                      "Failed to compile BPF filter (%s): %s",
    104                                       filter->filterstring, pcap_geterr(pcap));
     111                                      f->filterstring, pcap_geterr(pcap));
    105112                } else {
    106113                        /* Set the "flag" to indicate that the filterstring
    107114                         * has been compiled
    108115                         */
    109                         filter->flag = 1;
     116                        f->flag = 1;
    110117                }
    111118
     
    113120
    114121        }
     122
     123        if (FORMAT_DATA->filter != NULL)
     124                trace_destroy_filter(FORMAT_DATA->filter);
    115125
    116126        FORMAT_DATA->filter = filter;
     
    489499{
    490500        if (libtrace->format_data) {
     501                if (FORMAT_DATA->filter != NULL)
     502                        trace_destroy_filter(FORMAT_DATA->filter);
    491503
    492504                if (FORMAT_DATA->per_stream)
Note: See TracChangeset for help on using the changeset viewer.