Skip to content

Commit 5d21f84

Browse files
5.4.0 (#64)
* Add path similarity logic. Update flag 2048. * Remove ignore file logic on scan and update ignored extensions. * add lines coverage to snippet analysis. * Update Makefile, add live ldb version check * update help * solve minor bug with hints and dependencies tiebreak. * improve memory management for failed scans. * Solve memory segfault processing sbom.
1 parent ca30f9d commit 5d21f84

File tree

16 files changed

+394
-294
lines changed

16 files changed

+394
-294
lines changed

Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ endif
55
LDFLAGS+= -lldb -lm -lpthread -ldl
66

77
LDB_CURRENT_VERSION := $(shell ldb -v | sed 's/ldb-//' | head -c 3)
8-
LDB_TARGET_VERSION := 3.2
8+
LDB_TARGET_VERSION := 4.1
99

1010
VERSION_IS_LESS := $(shell echo $(LDB_CURRENT_VERSION) \< $(LDB_TARGET_VERSION) | bc)
11-
ifeq ($(VERSION_IS_LESS),1)
12-
LDFLAGS += -lcrypto -lz
13-
endif
1411

1512
CCFLAGS ?= -O -lz -Wall -Wno-unused-result -Wno-deprecated-declarations -g -Iinc -Iexternal/inc -D_LARGEFILE64_SOURCE -D_GNU_SOURCE
1613
SOURCES=$(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard external/*.c) $(wildcard external/**/*.c)
@@ -20,8 +17,10 @@ TARGET=scanoss
2017

2118
# Regla de prueba
2219
$(TARGET): $(OBJECTS)
23-
@echo "Current version: $(LDB_CURRENT_VERSION)"
24-
@echo "LDFLAGS: $(LDFLAGS)"
20+
ifeq ($(VERSION_IS_LESS),1)
21+
@echo "Current LDB version: $(LDB_CURRENT_VERSION) is too old, please update to the lastest version to continue."
22+
exit 1
23+
endif
2524

2625
$(CC) -g -o $(TARGET) $^ $(LDFLAGS)
2726

inc/component.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct component_data_t
4141
char * dependency_text; /* used in json output generation */
4242
char * health_text; /* used in json output generation */
4343
int hits; /*used in binary analysis*/
44+
char * file_path_ref;
45+
int path_rank;
4446
} component_data_t;
4547

4648
component_data_t * component_init(void);

inc/scanoss.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#define WFP_REC_LN 18
4545

4646
/* Log files */
47-
#define SCANOSS_VERSION "5.3.5"
47+
#define SCANOSS_VERSION "5.4.0"
4848
#define SCAN_LOG "/tmp/scanoss_scan.log"
4949
#define MAP_DUMP "/tmp/scanoss_map.dump"
5050
#define SLOW_QUERY_LOG "/tmp/scanoss_slow_query.log"
@@ -65,7 +65,7 @@
6565
#define DISABLE_BEST_MATCH 256
6666
#define DISABLE_REPORT_IDENTIFIED 512
6767
#define ENABLE_DOWNLOAD_URL 1024
68-
#define ENABLE_GITHUB_FULL_PATH 2048
68+
#define ENABLE_PATH_HINT 2048
6969
#define DISABLE_SERVER_INFO 4096
7070
#define DISABLE_HEALTH 8192
7171
#define ENABLE_HIGH_ACCURACY 16384

src/binary_scan.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,11 @@ static bool get_all_file_ids(uint8_t *key, uint8_t *subkey, int subkey_ln, uint8
161161

162162
static void fhash_process(char * hash, component_list_t * comp_list)
163163
{
164-
struct ldb_table oss_fhash = {.db = "oss", .table = "fhashes", .key_ln = 16, .rec_ln = 0, .ts_ln = 2, .tmp = false};
164+
struct ldb_table oss_fhash = {.db = "oss", .table = "fhashes", .key_ln = 16, .rec_ln = 0, .ts_ln = 2, .tmp = false, .keys=2, .definitions = 0};
165+
166+
if (!ldb_table_exists(oss_fhash.db, oss_fhash.table)) // skip if the table is not present
167+
return;
168+
165169
uint8_t fhash[16];
166170
ldb_hex_to_bin(hash, 32, fhash);
167171
/* Get all file IDs for given wfp */
@@ -304,7 +308,10 @@ int binary_scan(char * input)
304308
break;
305309
component_list_destroy(result.components);
306310
free(result.file);
311+
result.file = NULL;
307312
free(result.md5);
313+
result.md5 = NULL;
314+
308315
sensibility++;
309316
};
310317

src/component.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ bool ignored_asset_match(uint8_t *url_record)
177177
return found;
178178
}
179179

180+
static char * look_for_version(char *in)
181+
{
182+
if (!in)
183+
return NULL;
184+
bool is_ver = false;
185+
186+
char *v = strstr(in, "-v");
187+
if (v && isdigit(*(v + 2)))
188+
is_ver = true;
189+
else
190+
{
191+
v = strchr(in, '.');
192+
if (v && isdigit(*(v + 1)) && (*(v + 2) == '.' || isdigit(*(v + 2))))
193+
is_ver = true;
194+
}
195+
196+
if (is_ver)
197+
{
198+
char * p = strchr(v, '/');
199+
if (p)
200+
return (p+1);
201+
}
202+
203+
return in;
204+
}
205+
180206
/**
181207
* @brief Fill the match structure
182208
* @param url_key md5 of the match url
@@ -203,7 +229,7 @@ bool fill_component(component_data_t *component, uint8_t *url_key, char *file_pa
203229
memcpy(component->url_md5, url_key, MD5_LEN);
204230
if (file_path)
205231
{
206-
component->file = strdup(file_path);
232+
component->file = strdup(look_for_version(file_path));
207233
component->path_ln = strlen(file_path);
208234
flip_slashes(component->file);
209235
}

src/file.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,27 +139,32 @@ void get_file_md5(char *filepath, uint8_t *md5_result)
139139

140140
/* Read file contents into buffer */
141141
FILE *in = fopen(filepath, "rb");
142+
143+
if (!in)
144+
{
145+
MD5(NULL, 0, md5_result);
146+
return;
147+
}
148+
142149
fseek(in, 0L, SEEK_END);
143150
long filesize = ftell(in);
144-
145151
if (!filesize)
146152
{
147153
MD5(NULL, 0, md5_result);
148154
}
149-
150155
else
151156
{
152157
/* Read file contents */
153158
fseek(in, 0L, SEEK_SET);
154159
uint8_t *buffer = malloc(filesize);
155-
if (!fread(buffer, filesize, 1, in)) fprintf(stderr, "Warning: cannot open file %s\n", filepath);
160+
if (!fread(buffer, filesize, 1, in))
161+
fprintf(stderr, "Warning: cannot open file %s\n", filepath);
156162

157163
/* Calculate MD5sum */
158164
MD5(buffer, filesize, md5_result);
159-
free (buffer);
165+
free(buffer);
166+
fclose(in);
160167
}
161-
162-
fclose(in);
163168
}
164169

165170
/**

src/help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Alternatively, these value can be written in %s\n\
8888
| 256 | Disable best match only (default: enabled) |\n\
8989
| 512 | Hide identified files (default: disabled) |\n\
9090
| 1024 | Enable download_url (default: disabled) |\n\
91-
| 2048 | Enable GitHub full path (default: disabled) |\n\
91+
| 2048 | Enable \"use path hint\" logic (default: disabled) |\n\
9292
| 4096 | Disable extended server stats (default: enabled) |\n\
9393
| 8192 | Disable health layer (default: enabled) |\n\
9494
| 16384 | Enable high accuracy, slower scan (default: disabled) |\n\

src/ignored_extensions.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ char *IGNORED_EXTENSIONS[] = {
3636

3737
/* File extensions */
3838
".1", ".2", ".3", ".4", ".5", ".6", ".7", ".8", ".9", ".ac", ".adoc", ".am",
39-
".asc", ".asciidoc", ".bmp", ".build", ".cfg", ".chm", ".class", ".cmake",
39+
".asc", ".asciidoc", ".bmp", ".build", ".cfg", ".chm", ".cmake",
4040
".cnf", ".conf", ".config", ".contributors", ".copying", ".crt", ".csproj",
41-
".css", ".csv", ".cvsignore", ".dat", ".data", ".db", ".doc", ".ds_store",
41+
".csv", ".cvsignore", ".dat", ".data", ".db", ".doc", ".ds_store",
4242
".dtd", ".dts", ".dtsi", ".dump", ".eot", ".eps", ".geojson", ".gdoc", ".gif",
4343
".gitignore", ".glif", ".gmo", ".gradle", ".guess", ".hex", ".htm", ".html",
4444
".ico", ".in", ".inc", ".info", ".ini", ".ipynb", ".jpeg", ".jpg", ".json",
@@ -49,7 +49,7 @@ char *IGNORED_EXTENSIONS[] = {
4949
".spec", ".sql", ".sub", ".svg", ".svn-base", ".tab", ".template", ".test",
5050
".tex", ".tiff", ".toml", ".ttf", ".txt", ".utf-8", ".vim", ".wav", ".whl",
5151
".woff", ".xht", ".xhtml", ".xls", ".xml", ".xpm", ".xsd", ".xul", ".yaml",
52-
".yml", ".LAS",".adk",".asc",".cif",".cli",".cosmo",".deploy",
52+
".yml", ".LAS",".adk",".asc",".cif",".cli",".cosmo",".deploy",".pom",
5353
".dfm",".dmm",".fa",".fasta",".fcb",".flm",".fna",".gbr",".gen",".gro",
5454
".hgtags",".hh",".ihex",".kp",".mpx",".pdb",".poly",".prn",".ps",".ref",
5555
".resx",".smp",".stg",".tfa",".tsv",".vcf",".vhd",".xy",".xyz",

src/ignorelist.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "ignorelist.h"
3737
#include "ignored_extensions.h"
38+
#include "debug.h"
3839

3940
/**
4041
* @brief Returns a pointer to the file extension of "path"
@@ -100,7 +101,11 @@ bool ignored_extension(char *name)
100101
{
101102
int i=0;
102103
while (IGNORED_EXTENSIONS[i])
103-
if (ends_with(IGNORED_EXTENSIONS[i++], name)) return true;
104+
if (ends_with(IGNORED_EXTENSIONS[i++], name))
105+
{
106+
scanlog("Component ignored by path extension: %s", name);
107+
return true;
108+
}
104109

105110
return false;
106111
}

src/main.c

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -65,91 +65,61 @@ component_item *declared_components;
6565
uint8_t trace_id[MD5_LEN];
6666
bool trace_on;
6767

68-
68+
#define LDB_VER_MIN "4.1.0"
6969
/* Initialize tables for the DB name indicated (defaults to oss) */
7070
void initialize_ldb_tables(char *name)
7171
{
72+
73+
char * ldb_ver = NULL;
74+
ldb_version(&ldb_ver);
75+
scanlog("ldb version: %s\n", ldb_ver);
76+
77+
if (!ldb_ver || strcmp(ldb_ver, LDB_VER_MIN) < 0)
78+
{
79+
fprintf(stderr, "The current ldb version %s is too old, please upgrade to %s to proceed\n", ldb_ver, LDB_VER_MIN);
80+
exit(EXIT_FAILURE);
81+
}
82+
free(ldb_ver);
83+
7284
char oss_db_name[MAX_ARGLN];
7385

7486
if (name) strcpy(oss_db_name, name);
7587
else strcpy(oss_db_name, DEFAULT_OSS_DB_NAME);
7688

77-
strcpy(oss_url.db, oss_db_name);
78-
strcpy(oss_url.table, "url");
79-
oss_url.key_ln = 16;
80-
oss_url.rec_ln = 0;
81-
oss_url.ts_ln = 2;
82-
oss_url.tmp = false;
83-
84-
strcpy(oss_file.db, oss_db_name);
85-
strcpy(oss_file.table, "file");
86-
oss_file.key_ln = 16;
87-
oss_file.rec_ln = 0;
88-
oss_file.ts_ln = 2;
89-
oss_file.tmp = false;
90-
91-
strcpy(oss_wfp.db, oss_db_name);
92-
strcpy(oss_wfp.table, "wfp");
93-
oss_wfp.key_ln = 4;
94-
oss_wfp.rec_ln = 18;
95-
oss_wfp.ts_ln = 2;
96-
oss_wfp.tmp = false;
97-
98-
strcpy(oss_purl.db, oss_db_name);
99-
strcpy(oss_purl.table, "purl");
100-
oss_purl.key_ln = 16;
101-
oss_purl.rec_ln = 0;
102-
oss_purl.ts_ln = 2;
103-
oss_purl.tmp = false;
104-
105-
strcpy(oss_copyright.db, oss_db_name);
106-
strcpy(oss_copyright.table, "copyright");
107-
oss_copyright.key_ln = 16;
108-
oss_copyright.rec_ln = 0;
109-
oss_copyright.ts_ln = 2;
110-
oss_copyright.tmp = false;
111-
112-
strcpy(oss_quality.db, oss_db_name);
113-
strcpy(oss_quality.table, "quality");
114-
oss_quality.key_ln = 16;
115-
oss_quality.rec_ln = 0;
116-
oss_quality.ts_ln = 2;
117-
oss_quality.tmp = false;
118-
119-
strcpy(oss_vulnerability.db, oss_db_name);
120-
strcpy(oss_vulnerability.table, "vulnerability");
121-
oss_vulnerability.key_ln = 16;
122-
oss_vulnerability.rec_ln = 0;
123-
oss_vulnerability.ts_ln = 2;
124-
oss_vulnerability.tmp = false;
125-
126-
strcpy(oss_dependency.db, oss_db_name);
127-
strcpy(oss_dependency.table, "dependency");
128-
oss_dependency.key_ln = 16;
129-
oss_dependency.rec_ln = 0;
130-
oss_dependency.ts_ln = 2;
131-
oss_dependency.tmp = false;
132-
133-
strcpy(oss_license.db, oss_db_name);
134-
strcpy(oss_license.table, "license");
135-
oss_license.key_ln = 16;
136-
oss_license.rec_ln = 0;
137-
oss_license.ts_ln = 2;
138-
oss_license.tmp = false;
139-
140-
strcpy(oss_attribution.db, oss_db_name);
141-
strcpy(oss_attribution.table, "attribution");
142-
oss_attribution.key_ln = 16;
143-
oss_attribution.rec_ln = 0;
144-
oss_attribution.ts_ln = 2;
145-
oss_attribution.tmp = false;
146-
147-
strcpy(oss_cryptography.db, oss_db_name);
148-
strcpy(oss_cryptography.table, "cryptography");
149-
oss_cryptography.key_ln = 16;
150-
oss_cryptography.rec_ln = 0;
151-
oss_cryptography.ts_ln = 2;
152-
oss_cryptography.tmp = false;
89+
char dbtable[MAX_ARGLN * 2];
90+
scanlog("Loading tables definitions\n");
91+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "url");
92+
oss_url = ldb_read_cfg(dbtable);
93+
94+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "file");
95+
oss_file = ldb_read_cfg(dbtable);
96+
97+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "wfp");
98+
oss_wfp = ldb_read_cfg(dbtable);
99+
100+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "purl");
101+
oss_purl = ldb_read_cfg(dbtable);
102+
103+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "copyright");
104+
oss_copyright = ldb_read_cfg(dbtable);
105+
106+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "quality");
107+
oss_quality = ldb_read_cfg(dbtable);
108+
109+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "vulnerability");
110+
oss_vulnerability = ldb_read_cfg(dbtable);
111+
112+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "dependency");
113+
oss_dependency = ldb_read_cfg(dbtable);
114+
115+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "license");
116+
oss_license = ldb_read_cfg(dbtable);
117+
118+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "attribution");
119+
oss_attribution = ldb_read_cfg(dbtable);
120+
121+
snprintf(dbtable, MAX_ARGLN * 2, "%s/%s", oss_db_name, "cryptography");
122+
oss_cryptography = ldb_read_cfg(dbtable);
153123

154124
kb_version_get();
155125
osadl_load_file();
@@ -304,12 +274,10 @@ int main(int argc, char **argv)
304274

305275
microseconds_start = microseconds_now();
306276

307-
initialize_ldb_tables(NULL);
308-
309277
/* Parse arguments */
310278
int option;
311279
bool invalid_argument = false;
312-
280+
char * ldb_db_name = NULL;
313281
while ((option = getopt(argc, argv, ":f:s:b:B:c:k:a:F:l:n:i:M:N:wtvhedqH")) != -1)
314282
{
315283
/* Check valid alpha is entered */
@@ -339,6 +307,7 @@ int main(int argc, char **argv)
339307
break;
340308

341309
case 'k':
310+
initialize_ldb_tables(ldb_db_name);
342311
mz_file_contents(optarg, oss_file.db);
343312
exit(EXIT_SUCCESS);
344313
break;
@@ -359,7 +328,7 @@ int main(int argc, char **argv)
359328
break;
360329

361330
case 'n':
362-
initialize_ldb_tables(optarg);
331+
ldb_db_name = strdup(optarg);
363332
break;
364333
case 'M':
365334
scan_max_snippets = atol(optarg);
@@ -475,6 +444,9 @@ int main(int argc, char **argv)
475444
exit(EXIT_FAILURE);
476445
}
477446

447+
initialize_ldb_tables(ldb_db_name);
448+
free(ldb_db_name);
449+
478450
/* Remove trailing backslashes from target (if any) */
479451
strcpy (target, argv[argc-1]);
480452
for (int i=strlen(target)-1; i>=0; i--) if (target[i]=='/') target[i]=0; else break;

0 commit comments

Comments
 (0)