From: "Mattias Rönnblom" <hofors@lysator.liu.se>
To: "Sachin Saxena (OSS)" <sachin.saxena@oss.nxp.com>,
dev@dpdk.org, stephen@networkplumber.org
Subject: Re: [v2] [RFC] dpaa2: replace system("echo ...") with file i/o
Date: Thu, 4 May 2023 09:36:56 +0200 [thread overview]
Message-ID: <42b3f9fc-ec61-0370-9a3d-af2101c8e712@lysator.liu.se> (raw)
In-Reply-To: <20230502095410.487696-1-sachin.saxena@oss.nxp.com>
On 2023-05-02 11:54, Sachin Saxena (OSS) wrote:
> From: Stephen Hemminger <stephen@networkplumber.org>
>
> 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 <stephen@networkplumber.org>
> Reviewed-by: Sachin Saxena <sachin.saxena@oss.nxp.com>
> ---
> drivers/bus/fslmc/portal/dpaa2_hw_dpio.c | 45 ++++++++++++++++--------
> 1 file changed, 31 insertions(+), 14 deletions(-)
>
> diff --git a/drivers/bus/fslmc/portal/dpaa2_hw_dpio.c b/drivers/bus/fslmc/portal/dpaa2_hw_dpio.c
> index 4aec7b2cd8..990cfc5d3b 100644
> --- a/drivers/bus/fslmc/portal/dpaa2_hw_dpio.c
> +++ b/drivers/bus/fslmc/portal/dpaa2_hw_dpio.c
> @@ -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 300
> uint32_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));
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())?
> + 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)
prev parent reply other threads:[~2023-05-04 7:37 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-02 21:49 Stephen Hemminger
2023-05-02 9:54 ` [v2] " Sachin Saxena (OSS)
2023-05-04 7:36 ` Mattias Rönnblom [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=42b3f9fc-ec61-0370-9a3d-af2101c8e712@lysator.liu.se \
--to=hofors@lysator.liu.se \
--cc=dev@dpdk.org \
--cc=sachin.saxena@oss.nxp.com \
--cc=stephen@networkplumber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).