dpaa2: replace system("echo ...") with file i/o
RFC 1/1 dpaa2: replace system("echo ...") with file i/o
Using system() is a bad idea in driver code because it introduces a number of potential security issues. The codeql analysis tool flags this a potential security issue.
Instead just use normal stdio to do the same thing.
Compile test only, do not have this hardware and therefore can not test this.
--- drivers/bus/fslmc/portal/dpaa2_hw_dpio.c | 33 +++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-)
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +21 -12
@@ -125,12 +125,12 @@ static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) {#define STRING_LEN 28 -#define COMMAND_LEN 50 +#define AFFINITY_LEN 128uint32_t cpu_mask =1 ; -int ret;size_t len =0 ;char *temp =NULL , *token =NULL ; -char string[STRING_LEN ], command[COMMAND_LEN ]; +char string[STRING_LEN ]; +char smp_affinity[AFFINITY_LEN ];FILE *file;snprintf (string,STRING_LEN ,"dpio.%d" , dpio_id);
@@ -153,18 +153,27 @@ dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id)fclose (file);return ; } +free (temp); +fclose (file); +snprintf (smp_affinity,AFFINITY_LEN , +"/proc/irq/%s/smp_affinity" , token); cpu_mask = cpu_mask << cpu_id; -snprintf (command,COMMAND_LEN ,"echo %X > /proc/irq/%s/smp_affinity" , - cpu_mask, token); - ret =system (command); -if (ret <0 ) -DPAA2_BUS_DEBUG ( -"Failed to affine interrupts on respective core" ); -else -DPAA2_BUS_DEBUG (" %s command is executed" , command); -free (temp); + file =fopen (smp_affinity,"w" ); +if (file ==NULL ) { +DPAA2_BUS_WARN ("Failed to open %s" , smp_affinity); +return ; + } +fprintf (file,"%X\n" , cpu_mask); +fflush (file); + +if (ferror (file)) { +fclose (file); +DPAA2_BUS_WARN ("Failed to write to %s" , smp_affinity); +return ; + } +fclose (file); }
Signature · 1 line
2.35.1
RFC v2 1/1 dpaa2: replace system("echo ...") with file i/o
From: Stephen Hemminger <[email protected]>
Using system() is a bad idea in driver code because it introduces a number of potential security issues. The codeql analysis tool flags this a potential security issue.
Instead just use normal stdio to do the same thing.
Compile test only, do not have this hardware and therefore can not test this.
--- drivers/bus/fslmc/portal/dpaa2_hw_dpio.c | 45 ++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-)
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +31 -14
@@ -125,14 +125,21 @@ static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) {#define STRING_LEN 28 -#define COMMAND_LEN 50 +#define AFFINITY_LEN 128 +#define CMD_LEN 300uint32_t cpu_mask =1 ; -int ret; -size_t len =0 ; -char *temp =NULL , *token =NULL ; -char string[STRING_LEN ], command[COMMAND_LEN ]; +size_t len =CMD_LEN ; +char *temp, *token =NULL ; +char string[STRING_LEN ]; +char smp_affinity[AFFINITY_LEN ];FILE *file; + temp = (char *)malloc (len *sizeof (char )); +if (temp ==NULL ) { +DPAA2_BUS_WARN ("Unable to allocate temp buffer" ); +return ; + } +snprintf (string,STRING_LEN ,"dpio.%d" , dpio_id); file =fopen ("/proc/interrupts" ,"r" );if (!file) {
@@ -155,17 +162,27 @@ dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) } cpu_mask = cpu_mask << cpu_id; -snprintf (command,COMMAND_LEN ,"echo %X > /proc/irq/%s/smp_affinity" , - cpu_mask, token); - ret =system (command); -if (ret <0 ) -DPAA2_BUS_DEBUG ( -"Failed to affine interrupts on respective core" ); -else -DPAA2_BUS_DEBUG (" %s command is executed" , command); - +snprintf (smp_affinity,AFFINITY_LEN , +"/proc/irq/%s/smp_affinity" , token); +/* Free 'temp' memory after using the substring 'token' */ free (temp);fclose (file); + + file =fopen (smp_affinity,"w" ); +if (file ==NULL ) { +DPAA2_BUS_WARN ("Failed to open %s" , smp_affinity); +return ; + } +fprintf (file,"%X\n" , cpu_mask); +fflush (file); + +if (ferror (file)) { +fclose (file); +DPAA2_BUS_WARN ("Failed to write to %s" , smp_affinity); +return ; + } + +fclose (file); }static int dpaa2_dpio_intr_init (struct dpaa2_dpio_dev *dpio_dev)
Signature · 1 line
2.25.1
Quoted 30 lines · Sachin Saxena (OSS)
From: Stephen Hemminger <[email protected]>
Using
system()is a bad idea in driver code because it introduces a number of potential security issues. The codeql analysis tool flags this a potential security issue.Instead just use normal stdio to do the same thing.
Compile test only, do not have this hardware and therefore can not test this.
Signed-off-by Stephen Hemminger <[email protected]>Reviewed-by Sachin Saxena <[email protected]>--- drivers/bus/fslmc/portal/dpaa2_hw_dpio.c | 45 ++++++++++++++++-------- 1 file changed, 31 insertions(+), 14 deletions(-)drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +2
-2@@ -125,14 +125,21 @@ static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) {#define STRING_LEN 28 -#define COMMAND_LEN 50 +#define AFFINITY_LEN 128 +#define CMD_LEN 300uint32_t cpu_mask =1 ; -int ret;
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c static void · line 137
Quoted 10 lines
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +5
-3-size_t len =0 ; -char *temp =NULL , *token =NULL ; -char string[STRING_LEN ], command[COMMAND_LEN ]; +size_t len =CMD_LEN ; +char *temp, *token =NULL ; +char string[STRING_LEN ]; +char smp_affinity[AFFINITY_LEN ];FILE *file; + temp = (char *)malloc (len *sizeof (char ));
No cast is necessary to go from void * to any other pointer.
sizeof(char) is by definition one. What you allocate from the heap are in units of chars, so multiplying with sizeof(char) doesn't make sense.
How is pre-allocating the buffer an improvement over the old code (deferring memory allocation to getline())?
Quoted 45 lines
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +24
-9+if (temp ==NULL ) { +DPAA2_BUS_WARN ("Unable to allocate temp buffer" ); +return ; + } +snprintf (string,STRING_LEN ,"dpio.%d" , dpio_id); file =fopen ("/proc/interrupts" ,"r" );if (!file) {@@ -155,17 +162,27 @@ dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) } cpu_mask = cpu_mask << cpu_id; -snprintf (command,COMMAND_LEN ,"echo %X > /proc/irq/%s/smp_affinity" , - cpu_mask, token); - ret =system (command); -if (ret <0 ) -DPAA2_BUS_DEBUG ( -"Failed to affine interrupts on respective core" ); -else -DPAA2_BUS_DEBUG (" %s command is executed" , command); - +snprintf (smp_affinity,AFFINITY_LEN , +"/proc/irq/%s/smp_affinity" , token); +/* Free 'temp' memory after using the substring 'token' */ free (temp);fclose (file); + + file =fopen (smp_affinity,"w" ); +if (file ==NULL ) { +DPAA2_BUS_WARN ("Failed to open %s" , smp_affinity); +return ; + } +fprintf (file,"%X\n" , cpu_mask); +fflush (file); + +if (ferror (file)) { +fclose (file); +DPAA2_BUS_WARN ("Failed to write to %s" , smp_affinity); +return ; + } + +fclose (file); }static int dpaa2_dpio_intr_init (struct dpaa2_dpio_dev *dpio_dev)
PATCH v3 1/1 bus/fslmc/dpaa2: replace system("echo ...") with file i/o
In reply to Stephen HemmingerUsing system() is a bad idea in driver code because it introduces a number of potential security issues. The codeql analysis tool flags this a potential security issue.
Instead just use normal stdio to do the same thing.
Compile test only, do not have this hardware and therefore can not test this.
---
v3 - remove unneccessary pre-allocation of the line buffer
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c | 32 +++++++++++++++--------- 1 file changed, 20 insertions(+), 12 deletions(-)
drivers/bus/fslmc/portal/dpaa2_hw_dpio.c +20 -12
@@ -125,12 +125,12 @@ static void dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) {#define STRING_LEN 28 -#define COMMAND_LEN 50 +#define AFFINITY_LEN 128uint32_t cpu_mask =1 ; -int ret;size_t len =0 ;char *temp =NULL , *token =NULL ; -char string[STRING_LEN ], command[COMMAND_LEN ]; +char string[STRING_LEN ]; +char smp_affinity[AFFINITY_LEN ];FILE *file;snprintf (string,STRING_LEN ,"dpio.%d" , dpio_id);
@@ -155,17 +155,25 @@ dpaa2_affine_dpio_intr_to_respective_core(int32_t dpio_id, int cpu_id) } cpu_mask = cpu_mask << cpu_id; -snprintf (command,COMMAND_LEN ,"echo %X > /proc/irq/%s/smp_affinity" , - cpu_mask, token); - ret =system (command); -if (ret <0 ) -DPAA2_BUS_DEBUG ( -"Failed to affine interrupts on respective core" ); -else -DPAA2_BUS_DEBUG (" %s command is executed" , command); - +snprintf (smp_affinity,AFFINITY_LEN ,"/proc/irq/%s/smp_affinity" , token);free (temp);fclose (file); + + file =fopen (smp_affinity,"w" ); +if (file ==NULL ) { +DPAA2_BUS_WARN ("Failed to open %s" , smp_affinity); +return ; + } +fprintf (file,"%X\n" , cpu_mask); +fflush (file); + +if (ferror (file)) { +fclose (file); +DPAA2_BUS_WARN ("Failed to write to %s" , smp_affinity); +return ; + } + +fclose (file); }static int dpaa2_dpio_intr_init (struct dpaa2_dpio_dev *dpio_dev)
Signature · 1 line
2.43.0
Thomas Monjalon
AppliedRe: [PATCH v3] bus/fslmc/dpaa2: replace system("echo ...") with file i/o
26/08/2024 18:11, Stephen Hemminger:
Quoted 11 lines
Using
system()is a bad idea in driver code because it introduces a number of potential security issues. The codeql analysis tool flags this a potential security issue.Instead just use normal stdio to do the same thing.
Compile test only, do not have this hardware and therefore can not test this.
Signed-off-by Stephen Hemminger <[email protected]>Reviewed-by Sachin Saxena <[email protected]>
Applied, thanks.