Skip to content

Commit c72481b

Browse files
committed
Make sure new files get created with secure premissions.
1 parent 50ca300 commit c72481b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

jpegoptim.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*******************************************************************
22
* JPEGoptim
3-
* Copyright (c) Timo Kokkonen, 1996-2023.
3+
* Copyright (c) Timo Kokkonen, 1996-2025.
44
* All Rights Reserved.
55
*
66
* requires libjpeg (Independent JPEG Group's JPEG software
@@ -64,7 +64,7 @@
6464
#include "jpegoptim.h"
6565

6666

67-
#define VERSION "1.5.5"
67+
#define VERSION "1.5.6beta"
6868
#define COPYRIGHT "Copyright (C) 1996-2023, Timo Kokkonen"
6969

7070
#if HAVE_WAIT && HAVE_FORK
@@ -1021,7 +1021,7 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
10211021
if (copy_file(newname,tmpfilename))
10221022
fatal("%s, failed to create backup: %s",
10231023
(stdin_mode ? "stdin" : filename), tmpfilename);
1024-
if ((outfile=fopen(newname,"wb"))==NULL)
1024+
if ((outfile=create_file(newname))==NULL)
10251025
fatal("%s, error opening output file: %s",
10261026
(stdin_mode ? "stdin" : filename), newname);
10271027
outfname=newname;
@@ -1044,7 +1044,7 @@ int optimize(FILE *log_fh, const char *filename, const char *newname,
10441044
snprintf(tmpfilename,sizeof(tmpfilename),
10451045
"%sjpegoptim-%d-%d.%ld.tmp", tmpdir,
10461046
(int)getuid(), (int)getpid(), (long)time(NULL));
1047-
if ((outfile = fopen(tmpfilename,"wb")) == NULL)
1047+
if ((outfile = create_file(tmpfilename)) == NULL)
10481048
#endif
10491049
fatal("error opening temporary file: %s", tmpfilename);
10501050
outfname=tmpfilename;

jpegoptim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ extern int quiet_mode;
6464

6565

6666
/* misc.c */
67+
FILE* create_file(const char *name);
6768
int delete_file(const char *name);
6869
long file_size(FILE *fp);
6970
int is_directory(const char *path);

misc.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* misc.c
22
*
3-
* Copyright (C) 1996-2022 Timo Kokkonen
3+
* Copyright (C) 1996-2025 Timo Kokkonen
44
* All Rights Reserved.
55
*
66
* SPDX-License-Identifier: GPL-3.0-or-later
@@ -25,6 +25,7 @@
2525
#include "config.h"
2626
#endif
2727
#include <stdio.h>
28+
#include <fcntl.h>
2829
#include <sys/types.h>
2930
#include <sys/stat.h>
3031
#ifdef HAVE_UNISTD_H
@@ -38,6 +39,24 @@
3839
#include "jpegoptim.h"
3940

4041

42+
FILE* create_file(const char *name)
43+
{
44+
FILE *f;
45+
int fd;
46+
47+
if (!name)
48+
return NULL;
49+
50+
if ((fd = open(name, (O_WRONLY | O_CREAT | O_TRUNC), (S_IWUSR | S_IRUSR))) < 0)
51+
return NULL;
52+
if (!(f = fdopen(fd, "wb"))) {
53+
close(fd);
54+
return NULL;
55+
}
56+
57+
return f;
58+
}
59+
4160
int delete_file(const char *name)
4261
{
4362
int retval;
@@ -132,13 +151,11 @@ int copy_file(const char *srcfile, const char *dstfile)
132151
if (!srcfile || !dstfile)
133152
return -1;
134153

135-
in=fopen(srcfile,"rb");
136-
if (!in) {
154+
if (!(in = fopen(srcfile, "rb"))) {
137155
warn("failed to open file for reading: %s", srcfile);
138156
return -2;
139157
}
140-
out=fopen(dstfile,"wb");
141-
if (!out) {
158+
if (!(out = create_file(dstfile))) {
142159
fclose(in);
143160
warn("failed to open file for writing: %s", dstfile);
144161
return -3;

0 commit comments

Comments
 (0)