Coverage Report

Created: 2025-07-11 06:08

/src/yara/libyara/proc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
Copyright (c) 2007-2013. The YARA Authors. All Rights Reserved.
3
4
Redistribution and use in source and binary forms, with or without modification,
5
are permitted provided that the following conditions are met:
6
7
1. Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
9
10
2. Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation and/or
12
other materials provided with the distribution.
13
14
3. Neither the name of the copyright holder nor the names of its contributors
15
may be used to endorse or promote products derived from this software without
16
specific prior written permission.
17
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#include <assert.h>
31
#include <yara/error.h>
32
#include <yara/exec.h>
33
#include <yara/globals.h>
34
#include <yara/mem.h>
35
#include <yara/proc.h>
36
37
int _yr_process_attach(int, YR_PROC_ITERATOR_CTX*);
38
int _yr_process_detach(YR_PROC_ITERATOR_CTX*);
39
40
YR_API int yr_process_open_iterator(int pid, YR_MEMORY_BLOCK_ITERATOR* iterator)
41
0
{
42
0
  YR_DEBUG_FPRINTF(2, stderr, "+ %s(pid=%d) {\n", __FUNCTION__, pid);
43
44
0
  int result = ERROR_INTERNAL_FATAL_ERROR;
45
46
0
  YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) yr_malloc(
47
0
      sizeof(YR_PROC_ITERATOR_CTX));
48
49
0
  if (context == NULL)
50
0
  {
51
0
    result = ERROR_INSUFFICIENT_MEMORY;
52
0
    goto _exit;
53
0
  }
54
55
0
  iterator->context = context;
56
0
  iterator->first = yr_process_get_first_memory_block;
57
0
  iterator->next = yr_process_get_next_memory_block;
58
0
  iterator->last_error = ERROR_SUCCESS;
59
60
  // In a process scan file size is undefined, when the file_size function is
61
  // set to NULL the value returned by the filesize keyword is YR_UNDEFINED.
62
0
  iterator->file_size = NULL;
63
64
0
  context->buffer = NULL;
65
0
  context->buffer_size = 0;
66
0
  context->current_block.base = 0;
67
0
  context->current_block.size = 0;
68
0
  context->current_block.context = context;
69
0
  context->current_block.fetch_data = yr_process_fetch_memory_block_data;
70
0
  context->proc_info = NULL;
71
72
0
  GOTO_EXIT_ON_ERROR_WITH_CLEANUP(
73
0
      _yr_process_attach(pid, context), yr_free(context));
74
75
0
  result = ERROR_SUCCESS;
76
77
0
_exit:
78
79
0
  YR_DEBUG_FPRINTF(2, stderr, "} = %d // %s()\n", result, __FUNCTION__);
80
81
0
  return result;
82
0
}
83
84
YR_API int yr_process_close_iterator(YR_MEMORY_BLOCK_ITERATOR* iterator)
85
0
{
86
0
  YR_DEBUG_FPRINTF(2, stderr, "- %s() {}\n", __FUNCTION__);
87
88
0
  YR_PROC_ITERATOR_CTX* context = (YR_PROC_ITERATOR_CTX*) iterator->context;
89
90
0
  if (context != NULL)
91
0
  {
92
0
    _yr_process_detach(context);
93
94
0
    if (context->buffer != NULL)
95
0
      yr_free((void*) context->buffer);
96
97
0
    yr_free(context->proc_info);
98
0
    yr_free(context);
99
100
0
    iterator->context = NULL;
101
0
  }
102
103
0
  return ERROR_SUCCESS;
104
0
}