Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a buffer overflow in restart summary read #950

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 23 additions & 28 deletions lib/resdata/rd_smspec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,15 +856,16 @@ static void rd_smspec_load_restart(rd_smspec_type *rd_smspec,
if (rd_file_has_kw(header, RESTART_KW)) {
const rd_kw_type *restart_kw =
rd_file_iget_named_kw(header, RESTART_KW, 0);
char tmp_base
[73]; /* To accomodate a maximum of 9 items which consist of 8 characters each. */
char *restart_base;
int i;
tmp_base[0] = '\0';
for (i = 0; i < rd_kw_get_size(restart_kw); i++)
strcat(tmp_base, (const char *)rd_kw_iget_ptr(restart_kw, i));

restart_base = util_alloc_strip_copy(tmp_base);
int num_blocks = rd_kw_get_size(restart_kw);
num_blocks = 0 ? num_blocks < 0 : num_blocks;
char *tmp_base = (char *)calloc(8 * num_blocks + 1, sizeof(char));
for (int i = 0; i < num_blocks; i++) {
const char *part = (const char *)rd_kw_iget_ptr(restart_kw, i);
strncat(tmp_base, part, 8);
}

char *restart_base = util_alloc_strip_copy(tmp_base);
free(tmp_base);
if (strlen(restart_base)) { /* We ignore the empty ones. */
char *smspec_header;

Expand Down Expand Up @@ -1198,14 +1199,7 @@ static bool rd_smspec_fread_header(rd_smspec_type *rd_smspec,
rd_smspec_type *rd_smspec_fread_alloc(const char *header_file,
const char *key_join_string,
bool include_restart) {
rd_smspec_type *rd_smspec;

{
char *path;
util_alloc_file_components(header_file, &path, NULL, NULL);
rd_smspec = rd_smspec_alloc_empty(false, key_join_string);
free(path);
}
rd_smspec_type *rd_smspec = rd_smspec_alloc_empty(false, key_join_string);

if (rd_smspec_fread_header(rd_smspec, header_file, include_restart)) {

Expand All @@ -1226,20 +1220,21 @@ rd_smspec_type *rd_smspec_fread_alloc(const char *header_file,

const rd::smspec_node *day_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "DAY");
if (day_node != NULL) {
rd_smspec->day_index = smspec_node_get_params_index(day_node);
rd_smspec->month_index = smspec_node_get_params_index(
&rd_smspec->misc_var_index["MONTH"]);
rd_smspec->year_index = smspec_node_get_params_index(
&rd_smspec->misc_var_index["YEAR"]);
const rd::smspec_node *month_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "MONTH");
const rd::smspec_node *year_node =
rd_smspec_get_var_node(rd_smspec->misc_var_index, "YEAR");

if (day_node != NULL && month_node != NULL && year_node != NULL) {
rd_smspec->day_index = day_node->get_params_index();
rd_smspec->month_index = month_node->get_params_index();
rd_smspec->year_index = year_node->get_params_index();
}

if ((rd_smspec->time_index == -1) && (rd_smspec->day_index == -1)) {
/* Unusable configuration.

Seems the restart file can also have time specified with
'YEARS' as basic time unit; that mode is not supported.
*/
// Unusable configuration.
// Seems the restart file can also have time specified with
// 'YEARS' as basic time unit; that mode is not supported.

util_abort("%s: Sorry the SMSPEC file seems to lack all time "
"information, need either TIME, or DAY/MONTH/YEAR "
Expand Down
1 change: 1 addition & 0 deletions python/resdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
alternative fails, the loader will try the default load behaviour
before giving up completely.
"""

import ctypes as ct
import os.path
import sys
Expand Down
1 change: 1 addition & 0 deletions python/resdata/geometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Simple package for working with 2D geometry.

"""

import resdata
from cwrap import Prototype

Expand Down
1 change: 1 addition & 0 deletions python/resdata/geometry/cpolyline.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Create a polygon
"""

import ctypes
import os.path

Expand Down
1 change: 1 addition & 0 deletions python/resdata/geometry/cpolyline_collection.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Create a polygon
"""

import ctypes

from cwrap import BaseCClass
Expand Down
1 change: 1 addition & 0 deletions python/resdata/gravimetry/rd_subsidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
different surveys. The implementation is a thin wrapper around the
rd_subsidence.c implementation in the resdata library.
"""

from cwrap import BaseCClass
from resdata import ResdataPrototype
from resdata.util.util import monkey_the_camel
Expand Down
1 change: 1 addition & 0 deletions python/resdata/grid/rd_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
implemented in the Grid class. The rd_grid module is a thin
wrapper around the rd_grid.c implementation from the resdata library.
"""

import ctypes

import warnings
Expand Down
1 change: 1 addition & 0 deletions python/resdata/grid/rd_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
When the selection process is complete the region instance can be
queried for the corresponding list of indices.
"""

from functools import wraps
import ctypes

Expand Down
1 change: 1 addition & 0 deletions python/resdata/rd_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
In addition to the enum definitions there are a few stateless
functions from rd_util.c which are not bound to any class type.
"""

from __future__ import absolute_import

import ctypes
Expand Down
1 change: 1 addition & 0 deletions python/resdata/resfile/fortio.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
support passing of FortIO handles to the underlying C functions. A
more extensive wrapping of the fortio implementation would be easy.
"""

import ctypes
import os

Expand Down
1 change: 1 addition & 0 deletions python/resdata/resfile/rd_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
[1]: In particular for restart files, which do not have a special
RestartFile class, there is some specialized functionality.
"""

import ctypes
import datetime
import re
Expand Down
1 change: 0 additions & 1 deletion python/resdata/summary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
used as basis for queries on summary vectors.
"""


import resdata.util.util
import resdata.geometry

Expand Down
1 change: 1 addition & 0 deletions python/resdata/util/util/stringlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
return a normal python list of string objects, used in this way you
hardly need to notice that the StringList class is at play.
"""

from resdata import ResdataPrototype
from cwrap import BaseCClass

Expand Down
2 changes: 2 additions & 0 deletions python/tests/rd_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ def test_identify_var_type(self):
)
self.assertEqual(Summary.varType("RPR"), SummaryVarType.RD_SMSPEC_REGION_VAR)
self.assertEqual(Summary.varType("WNEWTON"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(Summary.varType("YEAR"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(Summary.varType("MONTH"), SummaryVarType.RD_SMSPEC_MISC_VAR)
self.assertEqual(
Summary.varType("AARQ:4"), SummaryVarType.RD_SMSPEC_AQUIFER_VAR
)
Expand Down
4 changes: 2 additions & 2 deletions python/tests/rd_tests/test_sum_equinor.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ def test_ix_case(self):
)
self.assertIsNotNone(eclipse_summary)

hwell_padder = (
lambda key: key if key.split(":")[-1] != "HWELL_PR" else key + "OD"
hwell_padder = lambda key: (
key if key.split(":")[-1] != "HWELL_PR" else key + "OD"
)
self.assertEqual(
intersect_summary.keys("WWCT*"),
Expand Down
Loading