From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14085 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: "liucheng (G)" Newsgroups: gmane.linux.lib.musl.general Subject: [patch] return value of ulimit(UL_GETFSIZE) in X32 architecture Date: Thu, 25 Apr 2019 13:44:24 +0000 Message-ID: <869863DB5440B44FB22173F42FC3F3CE01CA3FC6@dggemm513-mbx.china.huawei.com> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="_000_869863DB5440B44FB22173F42FC3F3CE01CA3FC6dggemm513mbxchi_" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="87367"; mail-complaints-to="usenet@blaine.gmane.org" Cc: "liucheng (G)" To: "musl@lists.openwall.com" Original-X-From: musl-return-14101-gllmg-musl=m.gmane.org@lists.openwall.com Thu Apr 25 16:09:58 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1hJf4W-000MbH-Nr for gllmg-musl@m.gmane.org; Thu, 25 Apr 2019 16:09:56 +0200 Original-Received: (qmail 9435 invoked by uid 550); 25 Apr 2019 14:09:54 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 9269 invoked from network); 25 Apr 2019 13:44:44 -0000 Thread-Topic: [patch] return value of ulimit(UL_GETFSIZE) in X32 architecture Thread-Index: AdT7aWG/M0z10jt1RSKN2tuxhc9YFA== Accept-Language: zh-CN, en-US Content-Language: zh-CN x-originating-ip: [10.57.34.94] X-CFilter-Loop: Reflected Xref: news.gmane.org gmane.linux.lib.musl.general:14085 Archived-At: --_000_869863DB5440B44FB22173F42FC3F3CE01CA3FC6dggemm513mbxchi_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Dear ALL, Return value of ulimit(UL_GETFSIZE) in X32 architecture seems to be wrong. Here is the implementation of ulimit function in MUSL 1.1.22: #include #include #include long ulimit(int cmd, ...) { struct rlimit rl; getrlimit(RLIMIT_FSIZE, &rl); if (cmd =3D=3D UL_SETFSIZE) { long val; va_list ap; va_start(ap, cmd); val =3D va_arg(ap, long); va_end(ap); rl.rlim_cur =3D 512ULL * val; if (setrlimit(RLIMIT_FSIZE, &rl)) retu= rn -1; } return rl.rlim_cur / 512; } Make it simple in case of "cmd =3D UL_GETFSIZE", ulimit function becomes to= be: long ulimit(int cmd, ...) { struct rlimit rl; getrlimit(RLIMIT_FSIZE, &rl); return rl.rlim_cur / 512; } rl.rlim in ulimit function is the type of long long(8 Byte), however the re= turn value in X32 architecture is the type of long(4 Byte). So in that case, rl.rlim_cur / 512 would be larger than 0x7fffffff and ulim= it function returns -1. I also tried an experiment to improve my opinion as follows. [benchmark] $ cat ulimit_test.c #include #include int main() { printf("ret:%d\n", ulimit(UL_GETFSIZE)); return 0; } [testcase] Environment: Linux 4.4.171 #1 SMP Thu Apr 25 00:39:22 UTC 2019 armv7l GNU/L= inux $ /tmp # ulimit -f unlimited $ /tmp # ./ulimit_test ret:-1 $ /tmp # cp musl/libc.so /usr/lib/ // copy = a new MUSL libc.so with my [patch] $ /tmp # ./ulimit_test ret:2147483647 = // 0x7fffffff [patch] --- diff --git a/src/legacy/ulimit.c b/src/legacy/ulimit.c index 1f59e8e..d1620e6 100644 --- a/src/legacy/ulimit.c +++ b/src/legacy/ulimit.c @@ -1,6 +1,7 @@ #include #include #include +#include long ulimit(int cmd, ...) { @@ -15,5 +16,5 @@ long ulimit(int cmd, ...) rl.rlim_cur =3D 512ULL * val; if (setrlimit(RLIMIT_FSIZE, &rl)) return -1; } - return rl.rlim_cur / 512; + return rl.rlim_cur =3D=3D RLIM_INFINITY ? LONG_MAX : rl.rlim_cur / = 512; } --- Looking forward to your reply. Best regards. Cheng Liu --_000_869863DB5440B44FB22173F42FC3F3CE01CA3FC6dggemm513mbxchi_ Content-Type: text/html; charset="us-ascii" Content-Transfer-Encoding: quoted-printable

Dear ALL,

 

Retu= rn value of ulimit(UL_GETFSIZE) in X32 architecture seems to be wrong.=

 

Here= is the implementation of ulimit function in MUSL 1.1.22:=

#include <sys/resource.h>

#include <ulimit.h>

#include <stdarg.h>

 =

long ulimit(int cmd, ...)

{

     &nb= sp;           struct rlim= it rl;

    &nbs= p;            getrlimit(R= LIMIT_FSIZE, &rl);

    &nbs= p;            if (cmd =3D= =3D UL_SETFSIZE) {

    &nbs= p;              = ;      long val;

    &nbs= p;              = ;      va_list ap;

    &nbs= p;              = ;      va_start(ap, cmd);

    &nbs= p;              = ;      val =3D va_arg(ap, long);

    &nbs= p;              = ;      va_end(ap);

    &nbs= p;              = ;      rl.rlim_cur =3D 512ULL * val;

    &nbs= p;             =             &nb= sp;      if (setrlimit(RLIMIT_FSIZE, &rl)) ret= urn -1;

    &nbs= p;            }

    &nbs= p;            return rl.r= lim_cur / 512;

}

 

Make= it simple in case of “cmd =3D UL_GETFSIZE”, ulimit function be= comes to be:

    &nbs= p;    long ulimit(int cmd, …)

    &nbs= p;    {

    &nbs= p;            &= nbsp; struct rlimit rl;

    &nbs= p;            &= nbsp; getrlimit(RLIMIT_FSIZE, &rl);

    &nbs= p;            &= nbsp; return rl.rlim_cur / 512;

}

 

rl.r= lim in ulimit function is the type of long long(8 Byte), however the return= value in X32 architecture is the type of long(4 Byte).

So i= n that case, rl.rlim_cur / 512 would be larger than 0x7fffffff and ulimit f= unction returns -1.

 

 

I al= so tried an experiment to improve my opinion as follows.<= /p>

 

[= benchmark]

$ cat ulimit_test.c

#include <stdio.h>

#include <ulimit.h>

 =

int main()=

{

    &nbs= p;   printf("ret:%d\n", ulimit(UL_GETFSIZE));

 =

    &nbs= p;   return 0;

}

 

[= testcase]

Envi= ronment: Linux 4.4.171 #1 SMP Thu Apr 25 00:39:22 UTC 2019 armv7l GNU/Linux=

 

$ /tmp # ulimit -f

unlimited<= /i>

$ /tmp # ./ulimit_test<= /o:p>

ret:-1=

$ /tmp # cp musl/libc.so /us= r/lib/           &nb= sp;            =          // copy a new MUSL libc.so with my <= b>[patch]

$ /tmp # ./ulimit_test<= /o:p>

ret:2147483647  &n= bsp;            = ;            &n= bsp;            = ;            &n= bsp;          // 0x7fffffff

 

[= patch]

---

diff --git a/src/legacy/ulim= it.c b/src/legacy/ulimit.c

index 1f59e8e..d1620e6 10064= 4

--- a/src/legacy/ulimit.c

+++ b/src/legacy= /ulimit.c

@@ -1,6 +1,7 @@

#include <sys/resource.h&= gt;

#include <ulimit.h>

#include <stdarg.h>

+#include <limits.h&g= t;

 long ulimit(int cmd, .= ..)

{

@@ -15,5 +16,5 @@ long u= limit(int cmd, ...)

    &nbs= p;           rl.rlim_cur = =3D 512ULL * val;

    &nbs= p;           if (setrlimi= t(RLIMIT_FSIZE, &rl)) return -1;

    &nbs= p;  }

-    &nb= sp;   return rl.rlim_cur / 512;

+    = ;   return rl.rlim_cur =3D=3D RLIM_INFINITY ? LONG_MAX : rl.rlim_= cur / 512;

}

---

 =

Look= ing forward to your reply.

Best= regards.

Chen= g Liu

 =

--_000_869863DB5440B44FB22173F42FC3F3CE01CA3FC6dggemm513mbxchi_--