Return-Path: Received: from mail-co1nam03on0130.outbound.protection.outlook.com ([104.47.40.130]:11340 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032234AbeCAPbF (ORCPT ); Thu, 1 Mar 2018 10:31:05 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: Jiri Slaby , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [added to the 4.1 stable tree] ipc: msg, make msgrcv work with LONG_MIN Date: Thu, 1 Mar 2018 15:24:42 +0000 Message-ID: <20180301152116.1486-215-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Jiri Slaby This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 999898355e08ae3b92dfd0a08db706e0c6703d30 ] When LONG_MIN is passed to msgrcv, one would expect to recieve any message. But convert_mode does *msgtyp =3D -*msgtyp and -LONG_MIN is undefined. In particular, with my gcc -LONG_MIN produces -LONG_MIN again. So handle this case properly by assigning LONG_MAX to *msgtyp if LONG_MIN was specified as msgtyp to msgrcv. This code: long msg[] =3D { 100, 200 }; int m =3D msgget(IPC_PRIVATE, IPC_CREAT | 0644); msgsnd(m, &msg, sizeof(msg), 0); msgrcv(m, &msg, sizeof(msg), LONG_MIN, 0); produces currently nothing: msgget(IPC_PRIVATE, IPC_CREAT|0644) =3D 65538 msgsnd(65538, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, 0) =3D 0 msgrcv(65538, ... Except a UBSAN warning: UBSAN: Undefined behaviour in ipc/msg.c:745:13 negation of -9223372036854775808 cannot be represented in type 'long int'= : With the patch, I see what I expect: msgget(IPC_PRIVATE, IPC_CREAT|0644) =3D 0 msgsnd(0, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, 0) =3D 0 msgrcv(0, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, -9223372036854= 775808, 0) =3D 16 Link: http://lkml.kernel.org/r/20161024082633.10148-1-jslaby@suse.cz Signed-off-by: Jiri Slaby Cc: Davidlohr Bueso Cc: Manfred Spraul Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- ipc/msg.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ipc/msg.c b/ipc/msg.c index 3b2b0f5149ab..55730c74a42a 100644 --- a/ipc/msg.c +++ b/ipc/msg.c @@ -740,7 +740,10 @@ static inline int convert_mode(long *msgtyp, int msgfl= g) if (*msgtyp =3D=3D 0) return SEARCH_ANY; if (*msgtyp < 0) { - *msgtyp =3D -*msgtyp; + if (*msgtyp =3D=3D LONG_MIN) /* -LONG_MIN is undefined */ + *msgtyp =3D LONG_MAX; + else + *msgtyp =3D -*msgtyp; return SEARCH_LESSEQUAL; } if (msgflg & MSG_EXCEPT) --=20 2.14.1