From a7c1d0adfda3c326fde19746d3ff62f1b5b447dc Mon Sep 17 00:00:00 2001 From: Karol Babioch Date: Fri, 4 May 2018 16:16:39 +0200 Subject: [PATCH] Check whether fullpath can be constructed correctly This checks the return code of snprintf() and makes sure that fullpath actually fits into a buffer of size PATH_MAX. Otherwise, it will output an error and exit. --- ykpamcfg.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ykpamcfg.c b/ykpamcfg.c index fece317..0a56d73 100644 --- a/ykpamcfg.c +++ b/ykpamcfg.c @@ -166,8 +166,13 @@ do_add_hmac_chalresp(YK_KEY *yk, uint8_t slot, bool verbose, char *output_dir, u if (!output_dir){ char fullpath[PATH_MAX]; - snprintf(fullpath, PATH_MAX, "%s/.yubico", p->pw_dir); - + int i = snprintf(fullpath, PATH_MAX, "%s/.yubico", p->pw_dir); + + if (i < 0 || i >= PATH_MAX) { + fprintf(stderr, "Failed to construct fullpath: %s\n", p->pw_dir); + goto out; + } + //check if directory exists if (stat(fullpath,&st)!=0 ){ if(mkdir(fullpath, S_IRWXU)==-1){