Linux Test Project
 help / color / mirror / Atom feed
From: Richard Palethorpe <rpalethorpe@suse.de>
To: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v3 04/16] API/cgroup: Implement tst_cg_load_config
Date: Mon, 07 Mar 2022 09:05:21 +0000	[thread overview]
Message-ID: <871qzeniu7.fsf@suse.de> (raw)
In-Reply-To: <baec527891fe83e75958f9db3634f1a0a828bf07.1646434670.git.luke.nowakowskikrijger@canonical.com>

Hello Luke,

Really great work for the patch series in general! However see comments
below.

Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com> writes:

> Implement tst_cg_load_config which consumes the state given by
> tst_cg_print_config to update the internal test state to reflect
> the given config.
>
> This allows for programs using the cgroup C API to load and reload
> state, allowing functionality such as calling tst_cg_require and
> tst_cg_cleanup to function properly between programs or between
> invocations of a binary using the C API.
>
> Signed-off-by: Luke Nowakowski-Krijger <luke.nowakowskikrijger@canonical.com>
> ---
> v2: Add root null check in parse_root_config.
>     Remove checking for ltp_drain_dir key from config as it was
>     redundant.
>     Remove unsued variable in parse_ctrl_config.
>     Cleanup some compiler warnings.
> v3: Rewrite to consume each line of the config with a scanf to make
>     the parsing much simpler while using new config variables.
>
>  include/tst_cgroup.h |  7 +++++
>  lib/tst_cgroup.c     | 62 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 69 insertions(+)
>
> diff --git a/include/tst_cgroup.h b/include/tst_cgroup.h
> index 87e55f4df..9bad0d366 100644
> --- a/include/tst_cgroup.h
> +++ b/include/tst_cgroup.h
> @@ -121,6 +121,13 @@ void tst_cg_scan(void);
>   */
>  void tst_cg_print_config(void);
>  
> +/* Load the config printed out by tst_cg_print_config and configure the internal
> + * libary state to match the config. Used to allow tst_cg_cleanup to properly
> + * cleanup mounts and directories created by tst_cg_require between program
> + * invocations.
> + */
> +void tst_cg_load_config(const char *const config);
> +
>  /* Ensure the specified controller is available in the test's default
>   * CGroup, mounting/enabling it if necessary. Usually this is not
>   * necesary use tst_test.needs_cgroup_controllers instead.
> diff --git a/lib/tst_cgroup.c b/lib/tst_cgroup.c
> index 8f95064b3..90d91d94e 100644
> --- a/lib/tst_cgroup.c
> +++ b/lib/tst_cgroup.c
> @@ -366,6 +366,68 @@ static struct cgroup_root *cgroup_find_root(const char *const mnt_path)
>  	return NULL;
>  }
>  
> +static void parse_config(const char *const config_entry)

cgroup_parse_config_line perhaps? or cgroup_parse_ctrl?

> +{
> +	const char ctrl_name[32], mnt_path[PATH_MAX],
> test_dir_name[NAME_MAX + 1];

These buffers are not const.

I have lsp and clangd setup in emacs which warns about this. It's easy
to miss the warning in the compiler output.

> +	int ver, we_require_it, we_mounted_it, ltp_dir_we_created_it;
> +	struct cgroup_root *root;
> +	struct cgroup_ctrl *ctrl;
> +
> +	sscanf(config_entry, CONFIG_FORMAT,
> +		ctrl_name, &ver, &we_require_it, mnt_path, &we_mounted_it,
> +		&ltp_dir_we_created_it, test_dir_name);

You need to run 'make check-tst_cgroup' in ltp/lib. The sscanf return
value is not checked which could result in segfaults and other confusing
errors if parsing fails.

> +
> +	if (!(ctrl = cgroup_find_ctrl(ctrl_name)))

check_patch.pl forbids assignments in if statements (it makes static
analysis difficult). Again this is caught with 'make check'.

> +		tst_brk(TBROK, "Could not find ctrl from config. Ctrls changing between calls?");
> +
> +	ctrl->we_require_it = we_require_it;
> +
> +	if (!(root = cgroup_find_root(mnt_path)))
> +		tst_brk(TBROK, "Could not find root from config. Config possibly malformed?");
> +
> +	if (we_mounted_it)
> +		root->we_mounted_it = 1;
> +
> +	if (!root->ltp_dir.dir_name) {
> +		cgroup_dir_mk(&root->mnt_dir, cgroup_ltp_dir, &root->ltp_dir);
> +		cgroup_dir_mk(&root->ltp_dir, cgroup_ltp_drain_dir, &root->drain_dir);
> +		if (ltp_dir_we_created_it) {
> +			root->ltp_dir.we_created_it = 1;
> +			root->drain_dir.we_created_it = 1;
> +		}
> +	}
> +
> +	if (!root->test_dir.dir_name && strcmp(test_dir_name, "NULL")) {
> +		strncpy(cgroup_test_dir, test_dir_name, NAME_MAX);

I think this could result in an unterminated string. strncpy does not
add a null char after the string that was copied. Note also that the
format string passed to sscanf does not limit the field width so it
could overwrite root and ctrl on the stack if the input is long enough.

> +		cgroup_dir_mk(&root->ltp_dir, cgroup_test_dir, &root->test_dir);
> +		root->test_dir.we_created_it = 1;
> +	}
> +}
> +
> +/* Load the test state config provided by tst_cg_print_config
> + *
> + * This will reload some internal tst_cgroup state given by the config
> + * that might otherwise have been lost between calls or between different
> + * processes. In particular this is used by testcases/lib/tst_cgctl to
> + * provide access to this C api to shell scripts.
> + *
> + * The config keeps track of the minimal state needed for tst_cg_cleanup
> + * to cleanup mounts and directories created by tst_cg_require.
> + */
> +void tst_cg_load_config(const char *const config)
> +{
> +	char temp_config[BUFSIZ];
> +	char *line;
> +
> +	if (strlen(config) >= BUFSIZ)
> +		tst_brk(TBROK, "Config has exceeded buffer size?");
> +	strncpy(temp_config, config, BUFSIZ);

Again, this won't copy the null byte from config if strlen(config) ==
BUFSIZ. You could use memcpy here anyway if you know the string length.

> +
> +	line = strtok(temp_config, "\n");
> +	/* Make sure to consume the header. */
> +	for (line = strtok(NULL, "\n"); line; line = strtok(NULL, "\n"))
> +		parse_config(line);
> +}
>  
>  /* Determine if a mounted cgroup hierarchy is unique and record it if so.
>   *


-- 
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  reply	other threads:[~2022-03-07 10:44 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 23:18 [LTP] [PATCH v2 00/16] Expand Cgroup lib and modify controller tests Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v3 01/16] API/cgroup: Modify tst_cg_print_config for parsing and consumption Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH 02/16] API/cgroup: Add option for specific pid to tst_cg_opts Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 03/16] API/cgroup: Add cgroup_find_root helper function Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v3 04/16] API/cgroup: Implement tst_cg_load_config Luke Nowakowski-Krijger
2022-03-07  9:05   ` Richard Palethorpe [this message]
2022-03-16 22:38     ` Luke Nowakowski-Krijger
2022-03-17  7:07       ` Richard Palethorpe
2022-04-27 17:38         ` Luke Nowakowski-Krijger
2022-04-28  9:17           ` Li Wang
2022-03-04 23:18 ` [LTP] [PATCH v2 05/16] API/cgroup: Add more controllers to tst_cgroup Luke Nowakowski-Krijger
2022-03-07 11:24   ` Richard Palethorpe
2022-03-16  9:35     ` Li Wang
2022-03-04 23:18 ` [LTP] [PATCH 06/16] API/cgroup: refuse to mount blkio when io controller is mounted Luke Nowakowski-Krijger
2022-03-16 10:01   ` Li Wang
2022-03-04 23:18 ` [LTP] [PATCH v2 07/16] testcases/lib: Implement tst_cgctl binary Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v3 08/16] controllers: Expand cgroup_lib shell library Luke Nowakowski-Krijger
2022-03-07 11:00   ` Richard Palethorpe
2022-03-07 11:39     ` Richard Palethorpe
2022-03-16  9:46       ` Li Wang
2022-03-16 21:46         ` Luke Nowakowski-Krijger
2022-03-17  5:38           ` Li Wang
2022-03-04 23:18 ` [LTP] [PATCH v2 09/16] controllers: Update cgroup_fj_* to use newer cgroup lib and test lib Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 10/16] controllers: Update memcg_control_test to newer test lib and cgroup lib Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 11/16] controllers: Update memcg/regression/* to new test " Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 12/16] controllers: Update memcg_stress_test to use newer " Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 13/16] controllers: update memcg/functional " Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 14/16] controllers: Update pids.sh " Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH v2 15/16] controllers: update cpuset_regression_test.sh " Luke Nowakowski-Krijger
2022-03-04 23:18 ` [LTP] [PATCH 16/16] controllers: update cgroup_regression_test " Luke Nowakowski-Krijger
2022-03-16  9:09   ` Li Wang

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=871qzeniu7.fsf@suse.de \
    --to=rpalethorpe@suse.de \
    --cc=ltp@lists.linux.it \
    --cc=luke.nowakowskikrijger@canonical.com \
    /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