elfint.c: Fix NULL pointer dereference issue in process_file function

Message ID 20241023100925.2381071-1-ant.v.moryakov@gmail.com
State Dropped
Delegated to: Mark Wielaard
Headers
Series elfint.c: Fix NULL pointer dereference issue in process_file function |

Commit Message

Anton Moryakov Oct. 23, 2024, 10:09 a.m. UTC
  From: AntonMoryakov <ant.v.moryakov@gmail.com>

fix: fixed null pointer inference error in process_file function

Fixed a bug that could cause the program to crash when processing files without a suffix.
Added a NULL check for the suffix pointer before calling stpcpy().
---
 src/elflint.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
  

Comments

Mark Wielaard Oct. 23, 2024, 12:54 p.m. UTC | #1
Hi Anton,

On Wed, 2024-10-23 at 13:09 +0300, ant.v.moryakov@gmail.com wrote:
> From: AntonMoryakov <ant.v.moryakov@gmail.com>
> 
> fix: fixed null pointer inference error in process_file function
> 
> Fixed a bug that could cause the program to crash when processing files without a suffix.

Do you have a testcase for this?

> Added a NULL check for the suffix pointer before calling stpcpy().

See comments below about where to put this check and the formatting.

Please also see the CONTRIBUTING file for how to submit patches for
inclusion. In particular the section "Sign your work"
https://sourceware.org/cgit/elfutils/tree/CONTRIBUTING

> ---
>  src/elflint.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/elflint.c b/src/elflint.c
> index cdc6108d..14346045 100644
> --- a/src/elflint.c
> +++ b/src/elflint.c
> @@ -257,7 +257,12 @@ process_file (int fd, Elf *elf, const char *prefix, const char *suffix,
>  	  {
>  	    cp = mempcpy (cp, prefix, prefix_len);
>  	    *cp++ = '(';
> -	    strcpy (stpcpy (new_suffix, suffix), ")");
> +		if(suffix != NULL){

Could this check go with the if statement just before this code?

>         /* Create the full name of the file.  */
>         if (prefix != NULL)

So that it reads if (prefix != NULL && suffix != NULL)

> +	    	strcpy (stpcpy (new_suffix, suffix), ")");
> +		}
> +		else{
> +			new_suffix[0] = '\0';
> +		}
>  	  }
>  	else
>  	  new_suffix[0] = '\0';

Note that the code formatting/indenting seems a little off.
In general the elfutils code follows the GNU coding standard
Formatting:
https://www.gnu.org/prep/standards/standards.html#Formatting

Thanks,

Mark
  

Patch

diff --git a/src/elflint.c b/src/elflint.c
index cdc6108d..14346045 100644
--- a/src/elflint.c
+++ b/src/elflint.c
@@ -257,7 +257,12 @@  process_file (int fd, Elf *elf, const char *prefix, const char *suffix,
 	  {
 	    cp = mempcpy (cp, prefix, prefix_len);
 	    *cp++ = '(';
-	    strcpy (stpcpy (new_suffix, suffix), ")");
+		if(suffix != NULL){
+	    	strcpy (stpcpy (new_suffix, suffix), ")");
+		}
+		else{
+			new_suffix[0] = '\0';
+		}
 	  }
 	else
 	  new_suffix[0] = '\0';