From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756358AbYEVQwu (ORCPT ); Thu, 22 May 2008 12:52:50 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1758280AbYEVQve (ORCPT ); Thu, 22 May 2008 12:51:34 -0400 Received: from mu-out-0910.google.com ([209.85.134.189]:44447 "EHLO mu-out-0910.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757024AbYEVQvb (ORCPT ); Thu, 22 May 2008 12:51:31 -0400 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer; b=EbRRN2hK97e186fckL1PsZ6yWDfVquVw8IC/5gqxVKxHs1v/HxfhRht9+NpuGUT083zwRk8rSD/bt3+4vtLu/Zlna2JryOeiLktG8uS4RStfHLdwGDMq85+E6RHSSn1DcnIfdMWY25hgpgkPGe4qJuus07HQKsMSOSWB3eO9PtI= From: Marcin Slusarz To: LKML Cc: Andrew Morton , Christoph Hellwig , Al Viro , Alexey Dobriyan , Johannes Weiner Subject: [PATCH] ERR_PTR: if errno value is known at compile time, make sure it's valid Date: Thu, 22 May 2008 18:50:18 +0200 Message-Id: <1211475019-5596-1-git-send-email-marcin.slusarz@gmail.com> X-Mailer: git-send-email 1.5.4.5 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org ERR_PTR is easy to call with wrong argument (positive errno), and this error lead to catastrophic event - oops or kernel panic (dereference of invalid pointer). As most of error handling code paths are rarely tested, this kind of bug can be hidden for years. Currently there are > 1400 calls of ERR_PTR with constant argument. Signed-off-by: Marcin Slusarz Cc: Andrew Morton Cc: Christoph Hellwig Cc: Al Viro Cc: Alexey Dobriyan Cc: Johannes Weiner --- include/linux/err.h | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-) diff --git a/include/linux/err.h b/include/linux/err.h index ec87f31..4773ed3 100644 --- a/include/linux/err.h +++ b/include/linux/err.h @@ -18,12 +18,21 @@ #ifndef __ASSEMBLY__ #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) +#define VALID_ERR_PTR_ARG(error) (error == 0 || IS_ERR_VALUE(error)) -static inline void *ERR_PTR(long error) +static inline void *__ERR_PTR(long error) { return (void *) error; } +/* + * implementation note: we have to make it a macro, otherwise + * gcc won't break the build on wrong argument + */ +#define ERR_PTR(error) (BUILD_BUG_ON(__builtin_constant_p(error) && \ + !VALID_ERR_PTR_ARG(error)), \ + __ERR_PTR(error)) + static inline long PTR_ERR(const void *ptr) { return (long) ptr; -- 1.5.4.5