Skip to content

Commit bbdfc1d

Browse files
olszomalmtrojnar
authored andcommitted
Avoid undefined behavior with BIO_get_fp by replacing BIO_new_file with fopen + BIO_new_fp
1 parent 5ac11e9 commit bbdfc1d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

appx.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,10 @@ static int get_current_position(BIO *bio, uint64_t *offset)
27442744
FILE *file = NULL;
27452745
int64_t pos;
27462746

2747-
BIO_get_fp(bio, &file);
2747+
if (BIO_get_fp(bio, &file) != 1 || file == NULL) {
2748+
fprintf(stderr, "BIO_get_fp() failed\n");
2749+
return 0; /* FAILED */
2750+
}
27482751
pos = ftello(file);
27492752
if (pos < 0) {
27502753
return 0; /* FAILED */

osslsigncode.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5110,6 +5110,7 @@ int main(int argc, char **argv)
51105110
DO_EXIT_0("Failed to read key or certificates\n");
51115111

51125112
if (options.cmd != CMD_VERIFY) {
5113+
FILE *fp;
51135114
/* Create message digest BIO */
51145115
hash = BIO_new(BIO_f_md());
51155116
#if defined(__GNUC__)
@@ -5123,13 +5124,17 @@ int main(int argc, char **argv)
51235124
#pragma GCC diagnostic pop
51245125
#endif
51255126
/* Create outdata file */
5126-
outdata = BIO_new_file(options.outfile, "w+bx");
5127-
if (!outdata && errno != EEXIST)
5128-
outdata = BIO_new_file(options.outfile, "w+b");
5129-
if (!outdata) {
5127+
fp = fopen(options.outfile, "w+b");
5128+
if (!fp) {
51305129
BIO_free_all(hash);
51315130
DO_EXIT_1("Failed to create file: %s\n", options.outfile);
51325131
}
5132+
outdata = BIO_new_fp(fp, BIO_CLOSE);
5133+
if (!outdata) {
5134+
fclose(fp);
5135+
BIO_free_all(hash);
5136+
DO_EXIT_1("Failed to wrap FILE in BIO: %s\n", options.outfile);
5137+
}
51335138
}
51345139
ctx = file_format_script.ctx_new(&options, hash, outdata);
51355140
if (!ctx)

0 commit comments

Comments
 (0)