public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Jan Stancek <jstancek@redhat.com>
To: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
Cc: ltp-list <ltp-list@lists.sourceforge.net>
Subject: Re: [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test
Date: Fri, 14 Feb 2014 05:54:26 -0500 (EST)	[thread overview]
Message-ID: <1615091378.3633979.1392375266510.JavaMail.zimbra@redhat.com> (raw)
In-Reply-To: <1392372746.2085.11.camel@G08JYZSD130126>





----- Original Message -----
> From: "Zeng Linggang" <zenglg.jy@cn.fujitsu.com>
> To: "ltp-list" <ltp-list@lists.sourceforge.net>
> Sent: Friday, 14 February, 2014 11:12:26 AM
> Subject: [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test
> 
> Add EPERM errno test for mlock(2).
> 
> Signed-off-by: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
> ---
>  testcases/kernel/syscalls/mlock/mlock02.c | 42
>  +++++++++++++++++++++++++++++--
>  1 file changed, 40 insertions(+), 2 deletions(-)
> 
> diff --git a/testcases/kernel/syscalls/mlock/mlock02.c
> b/testcases/kernel/syscalls/mlock/mlock02.c
> index a635d24..1e87777 100644
> --- a/testcases/kernel/syscalls/mlock/mlock02.c
> +++ b/testcases/kernel/syscalls/mlock/mlock02.c
> @@ -20,11 +20,15 @@
>   * ALGORITHM
>   * 	test 1:
>   *		Call mlock with a NULL address.  ENOMEM should be returned

Hi,

ENOMEM (Linux 2.6.9 and later) the caller had a non-zero RLIMIT_MEMLOCK soft resource limit,
       but tried to lock more memory than the limit permitted.
ENOMEM Some of the specified address range does not correspond to mapped pages in
       the address space of the process.

We seem to be mixing these two together. Will test 1 return ENOMEM
because we passed NULL or because RLIMIT_MEMLOCK was low enough?
Consider this example:

$ cat a.c
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/resource.h>
#include <stdio.h>

int main(void)
{
        char dummy[4096];
        struct rlimit rl;

        rl.rlim_cur = 1;
        rl.rlim_max = 1;

        if (mlock(NULL, 1024))
                perror("mlock(NULL, 1024)");

        if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0)
                perror("setrlimit");

        if (mlock(dummy, 1024))
                perror("mlock(dummy, 1024)");

        return 0;
}

$ ./a.out
mlock(NULL, 1024): Cannot allocate memory
mlock(dummy, 1024): Cannot allocate memory


> + *	test 2:
> + *		The  caller  was  not  privileged and its RLIMIT_MEMLOCK soft
> + *		resource limit was 0. EPERM should be returned
>   */
>  
>  #include <errno.h>
>  #include <unistd.h>
>  #include <sys/mman.h>
> +#include <pwd.h>
>  #include "test.h"
>  #include "usctest.h"
>  #include "safe_macros.h"
> @@ -32,20 +36,26 @@
>  char *TCID = "mlock02";
>  
>  static void *addr1;
> +static struct passwd *ltpuser;
>  static void setup(void);
>  static void mlock_verify(int);
> +static void setup2(void);
> +static void cleanup2(void);
>  static void cleanup(void);
>  
>  static struct test_case_t {
>  	void **addr;
>  	int len;
>  	int error;
> +	void (*setupfunc) ();
> +	void (*cleanupfunc) ();
>  } TC[] = {
> -	{&addr1, 1024, ENOMEM},
> +	{&addr1, 1024, ENOMEM, NULL, NULL},
> +	{&addr1, 1024, EPERM, setup2, cleanup2},
>  };
>  
>  int TST_TOTAL = ARRAY_SIZE(TC);
> -static int exp_enos[] = { ENOMEM, 0 };
> +static int exp_enos[] = { ENOMEM, EPERM, 0 };
>  
>  #if !defined(UCLINUX)
>  
> @@ -85,6 +95,8 @@ int main(void)
>  
>  static void setup(void)
>  {
> +	struct rlimit rl;
> +
>  	TEST_PAUSE;
>  
>  #ifdef __ia64__
> @@ -92,12 +104,28 @@ static void setup(void)
>  #else
>  	addr1 = NULL;
>  #endif
> +	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
> +
> +	rl.rlim_cur = 0;

rlim_max is not initialised, so it can fail if you run mlock02 as user
and it has non-zero value:
setrlimit(RLIMIT_MEMLOCK, {rlim_cur=0, rlim_max=4216848}) = -1 EPERM (Operation not permitted)

Initialising it to zero:
    +rl.rlim_max = 0;
will make the testcase work for root, but it's failing for unprivileged user:

$ ./mlock02
mlock02     1  TFAIL  :  mlock didn't fail as expected; expected - 12 : Cannot allocate memory: TEST_ERRNO=EPERM(1): Operation not permitted
mlock02     2  TBROK  :  seteuid failed at mlock02.c:146: errno=EPERM(1): Operation not permitted
mlock02     3  TBROK  :  Remaining cases broken

Regards,
Jan

> +
> +	if (setrlimit(RLIMIT_MEMLOCK, &rl) != 0) {
> +		tst_resm(TWARN,
> +			 "setrlimit failed to set the resource for "
> +			 "RLIMIT_MEMLOCK to check for mlock()");
> +		return;
> +	}
>  }
>  
>  static void mlock_verify(int i)
>  {
> +	if (TC[i].setupfunc != NULL)
> +		TC[i].setupfunc();
> +
>  	TEST(mlock(*(TC[i].addr), TC[i].len));
>  
> +	if (TC[i].cleanupfunc != NULL)
> +		TC[i].cleanupfunc();
> +
>  	if (TEST_RETURN != -1) {
>  		tst_resm(TFAIL, "mlock succeeded unexpectedly");
>  		return;
> @@ -112,6 +140,16 @@ static void mlock_verify(int i)
>  	}
>  }
>  
> +static void setup2(void)
> +{
> +	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
> +}
> +
> +static void cleanup2(void)
> +{
> +	SAFE_SETEUID(cleanup, 0);
> +}
> +
>  static void cleanup(void)
>  {
>  	TEST_CLEANUP;
> --
> 1.8.4.2
> 
> 
> 
> 
> ------------------------------------------------------------------------------
> Android apps run on BlackBerry 10
> Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
> Now with support for Jelly Bean, Bluetooth, Mapview and more.
> Get your Android app in front of a whole new audience.  Start now.
> http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
> _______________________________________________
> Ltp-list mailing list
> Ltp-list@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/ltp-list
> 

------------------------------------------------------------------------------
Android apps run on BlackBerry 10
Introducing the new BlackBerry 10.2.1 Runtime for Android apps.
Now with support for Jelly Bean, Bluetooth, Mapview and more.
Get your Android app in front of a whole new audience.  Start now.
http://pubads.g.doubleclick.net/gampad/clk?id=124407151&iu=/4140/ostg.clktrk
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

  reply	other threads:[~2014-02-14 10:54 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-14 10:10 [LTP] [PATCH] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-14 10:12 ` [LTP] [PATCH 2/2] mlock/mlock02.c: add EPERM errno test Zeng Linggang
2014-02-14 10:54   ` Jan Stancek [this message]
2014-02-19  9:38     ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-19  9:40       ` [LTP] [PATCH v2 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-19 11:29         ` Jan Stancek
2014-02-20  9:40           ` [LTP] [PATCH v3 1/2] mlock/mlock02.c: cleanup Zeng Linggang
2014-02-20  9:50             ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-20 11:05               ` Jan Stancek
2014-02-20 13:03                 ` Jan Stancek
2014-02-21  9:03                   ` Zeng Linggang
2014-03-03  7:47                   ` [LTP] [PATCH v4 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
2014-03-03  7:50                     ` [LTP] [PATCH v4 2/3] mlock/mlock02.c: cleanup Zeng Linggang
2014-03-03  7:51                     ` [LTP] [PATCH v4 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-03-03  9:07                       ` Jan Stancek
2014-03-03 11:22                         ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Zeng Linggang
2014-03-03 11:23                           ` [LTP] [PATCH v5 2/3] mlock/mlock02.c: cleanup Zeng Linggang
2014-03-03 11:25                           ` [LTP] [PATCH v5 3/3] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-03-03 20:28                             ` Jan Stancek
2014-03-04  5:02                               ` Zeng Linggang
2014-03-04  5:33                           ` [LTP] [PATCH v5 1/3] safe_macros: Add SAFE_GETRLIMIT and SAFE_SETRLIMIT Wanlong Gao
2014-03-04  6:17                             ` Zeng Linggang
2014-02-21  1:04                 ` [LTP] [PATCH v3 2/2] mlock/mlock02.c: add EPERM and ENOMEM errno tests Zeng Linggang
2014-02-19 11:02       ` [LTP] [PATCH v2 1/2] mlock/mlock02.c: cleanup Jan Stancek

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=1615091378.3633979.1392375266510.JavaMail.zimbra@redhat.com \
    --to=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    --cc=zenglg.jy@cn.fujitsu.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