Skip to content

Commit dd8976c

Browse files
committed
fix: prevent buffer overflow, change make file
- make now accounts for path separator in windows - cast size_t to int before comparison
1 parent fa61b47 commit dd8976c

File tree

6 files changed

+62
-34
lines changed

6 files changed

+62
-34
lines changed

β€ŽMakefile

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ BIN_DIR = bin
88

99
# Source files
1010
SRCS = $(wildcard $(SRC_DIR)/*.c) \
11-
$(wildcard $(SRC_DIR)/commands/*.c) \
12-
$(wildcard $(SRC_DIR)/utils/*.c)
11+
$(wildcard $(SRC_DIR)/commands/*.c) \
12+
$(wildcard $(SRC_DIR)/utils/*.c)
1313

1414
# Object files
1515
OBJS = $(SRCS:%.c=$(BUILD_DIR)/%.o)
@@ -20,35 +20,47 @@ TARGET = $(BIN_DIR)/gitkeykit
2020
# Platform-specific settings
2121
ifeq ($(OS),Windows_NT)
2222
TARGET := $(TARGET).exe
23-
RM = del /Q /F
24-
MKDIR = mkdir
23+
RM = cmd /C rmdir /S /Q
24+
MKDIR = cmd /C mkdir
25+
# Define Windows-specific directory creation
26+
directories:
27+
@if not exist "$(BUILD_DIR)" $(MKDIR) "$(BUILD_DIR)"
28+
@if not exist "$(BIN_DIR)" $(MKDIR) "$(BIN_DIR)"
29+
@if not exist "$(BUILD_DIR)\$(SRC_DIR)" $(MKDIR) "$(BUILD_DIR)\$(SRC_DIR)"
30+
@if not exist "$(BUILD_DIR)\$(SRC_DIR)\commands" $(MKDIR) "$(BUILD_DIR)\$(SRC_DIR)\commands"
31+
@if not exist "$(BUILD_DIR)\$(SRC_DIR)\utils" $(MKDIR) "$(BUILD_DIR)\$(SRC_DIR)\utils"
2532
else
2633
RM = rm -rf
2734
MKDIR = mkdir -p
35+
# Define Unix directory creation
36+
directories:
37+
$(MKDIR) $(BUILD_DIR)
38+
$(MKDIR) $(BIN_DIR)
39+
$(MKDIR) $(BUILD_DIR)/$(SRC_DIR)
40+
$(MKDIR) $(BUILD_DIR)/$(SRC_DIR)/commands
41+
$(MKDIR) $(BUILD_DIR)/$(SRC_DIR)/utils
2842
endif
2943

30-
# Default target
44+
# Add this near the top, after the variables but before the platform-specific settings
3145
all: directories $(TARGET)
3246

33-
# Create necessary directories
34-
directories:
35-
$(MKDIR) $(BUILD_DIR) $(BIN_DIR) \
36-
$(BUILD_DIR)/$(SRC_DIR) \
37-
$(BUILD_DIR)/$(SRC_DIR)/commands \
38-
$(BUILD_DIR)/$(SRC_DIR)/utils
39-
4047
# Link the final executable
4148
$(TARGET): $(OBJS)
4249
$(CC) $(OBJS) -o $(TARGET)
4350

4451
# Compile source files
4552
$(BUILD_DIR)/%.o: %.c
53+
ifeq ($(OS),Windows_NT)
54+
@if not exist "$(dir $@)" $(MKDIR) "$(subst /,\,$(dir $@))"
55+
else
4656
$(MKDIR) $(dir $@)
57+
endif
4758
$(CC) $(CFLAGS) -c $< -o $@
4859

4960
# Clean build files
5061
clean:
51-
$(RM) $(BUILD_DIR) $(BIN_DIR)
62+
$(RM) $(BUILD_DIR)
63+
$(RM) $(BIN_DIR)
5264

5365
# Install target (Unix-like systems only)
5466
install: all
@@ -58,4 +70,4 @@ install: all
5870
.PHONY: all clean directories install
5971

6072
# Dependencies
61-
-include $(OBJS:.o=.d)
73+
-include $(OBJS:.o=.d)

β€Žsrc/commands/reset.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22

33

44
int clear_gpg_config(void) {
5-
char gnupg_dir[512];
6-
char gpg_conf_path[512];
5+
char gnupg_dir[1024];
6+
char gpg_conf_path[1024];
77
char *home_dir;
88
struct stat st = {0};
99

1010
home_dir = getenv("HOME");
11-
if(!home_dir) {
11+
if (!home_dir) {
1212
fprintf(stderr, "Error: Could not get the home directory\n");
1313
return ERR_HOME_DIRECTORY_NOT_FOUND;
1414
}
1515

16-
snprintf(gnupg_dir, sizeof(gnupg_dir), "%s/.gnupg", home_dir);
16+
if (snprintf(gnupg_dir, sizeof(gnupg_dir), "%s/.gnupg", home_dir) >= (int)sizeof(gnupg_dir)) {
17+
fprintf(stderr, "Error: gnupg_dir buffer overflow\n");
18+
return ERR_BUFFER_OVERFLOW;
19+
}
20+
1721
if (stat(gnupg_dir, &st) == -1) {
1822
return SUCCESS; // No gnupg directory, so no config to clear
1923
}
20-
21-
snprintf(gpg_conf_path, sizeof(gpg_conf_path), "%s/gpg.conf", gnupg_dir);
22-
// Clearing gpg.conf by opening it in write mode and immediately closing it
24+
25+
if (snprintf(gpg_conf_path, sizeof(gpg_conf_path), "%s/gpg.conf", gnupg_dir) >= (int)sizeof(gpg_conf_path)) {
26+
fprintf(stderr, "Error: gpg_conf_path buffer overflow\n");
27+
return ERR_BUFFER_OVERFLOW;
28+
}
29+
2330
FILE *file = fopen(gpg_conf_path, "w");
2431
if (!file) {
2532
fprintf(stderr, "Error: Could not open gpg.conf for clearing\n");
@@ -29,9 +36,9 @@ int clear_gpg_config(void) {
2936

3037
printf("GPG configuration cleared.\n");
3138
return SUCCESS;
32-
3339
}
3440

41+
3542
int reset(void) {
3643
const char *cmd =
3744
"git config --global --unset user.name &&"

β€Žsrc/commands/start.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
int start(void) {
55
char gpg_path[128];
66
check_required_dependencies(gpg_path, sizeof(gpg_path));
7-
if (check_secret_keys(gpg_path) != SUCCESS) {
7+
if (check_secret_keys() != SUCCESS) {
88
create_pgp_key();
99
}
1010

β€Žsrc/gitkeykit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define ERR_GIT_CONFIG_RESET 9
4949
#define ERR_GPG_CONFIG_RESET 10
5050
#define ERR_HOME_DIRECTORY_NOT_FOUND 11
51+
#define ERR_BUFFER_OVERFLOW 12
5152

5253
// ++++ ++++ ++++ ++++ ++++ ++++ ++++
5354

@@ -59,7 +60,7 @@ int clear_gpg_config(void);
5960
int import_key(char * key_path);
6061
int check_git_installation(void);
6162
int set_git_config(char *gpg_path);
62-
int check_secret_keys(char *gpg_path);
63+
int check_secret_keys(void);
6364
int check_gpg_installation(char *gpg_path, size_t path_size);
6465
int check_required_dependencies(char *gpg_path, size_t path_size);
6566

β€Žsrc/utils/check_secret_keys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "../gitkeykit.h"
22

3-
int check_secret_keys(char *gpg_path) {
3+
int check_secret_keys(void) {
44
char command[256];
55
FILE *fp;
66

β€Žsrc/utils/linux_config.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
static int create_directory(const char *path) {
44
struct stat st = {0};
55
if (stat(path, &st) == -1) {
6-
return mkdir(path, 0700);
6+
#ifdef _WIN32
7+
return mkdir(path);
8+
#else
9+
return mkdir(path, 0700);
10+
#endif
711
}
812
return 0;
913
}
@@ -19,8 +23,8 @@ static int append_to_file(const char *filepath, const char *content) {
1923
}
2024

2125
int add_extra_config(void) {
22-
char gpg_conf_path[512];
23-
char gnupg_dir[512];
26+
char gpg_conf_path[1024];
27+
char gnupg_dir[1024];
2428
char *home_dir;
2529

2630
home_dir = getenv("HOME");
@@ -29,15 +33,21 @@ int add_extra_config(void) {
2933
return ERR_INVALID_INPUT;
3034
}
3135

32-
snprintf(gnupg_dir, sizeof(gnupg_dir), "%s/.gnupg", home_dir);
33-
snprintf(gpg_conf_path, sizeof(gpg_conf_path), "%s/gpg.conf", gnupg_dir);
36+
if (snprintf(gnupg_dir, sizeof(gnupg_dir), "%s/.gnupg", home_dir) >= (int)sizeof(gnupg_dir)) {
37+
fprintf(stderr, "Error: gnupg_dir buffer overflow\n");
38+
return ERR_BUFFER_OVERFLOW;
39+
}
40+
41+
if (snprintf(gpg_conf_path, sizeof(gpg_conf_path), "%s/gpg.conf", gnupg_dir) >= (int)sizeof(gpg_conf_path)) {
42+
fprintf(stderr, "Error: gpg_conf_path buffer overflow\n");
43+
return ERR_BUFFER_OVERFLOW;
44+
}
3445

3546
if (create_directory(gnupg_dir) != 0) {
3647
fprintf(stderr, "Error: Could not create .gnupg directory\n");
3748
return ERR_INVALID_INPUT;
3849
}
3950

40-
// Clear or create gpg.conf
4151
FILE *file = fopen(gpg_conf_path, "w");
4252
if (!file) {
4353
fprintf(stderr, "Error: Could not open gpg.conf\n");
@@ -51,14 +61,12 @@ int add_extra_config(void) {
5161
return ERR_INVALID_INPUT;
5262
}
5363

54-
// Kill gpg-agent
5564
if (system("gpgconf --kill gpg-agent") != 0) {
5665
fprintf(stderr, "Warning: Could not kill gpg-agent\n");
5766
} else {
5867
printf("gpg-agent killed.\n");
5968
}
6069

61-
// Start gpg-agent
6270
if (system("gpg-agent --daemon") != 0) {
6371
fprintf(stderr, "Warning: Could not start gpg-agent\n");
6472
} else {
@@ -76,4 +84,4 @@ int add_extra_config(void) {
7684
printf("╰─────────────────────────────────────────────╯\n");
7785

7886
return SUCCESS;
79-
}
87+
}

0 commit comments

Comments
Β (0)