From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <3okawXggKCusTcebPVQNTbbTYR.PbZYVONOVTNVYfbhePRjNeR.beT@flex--gprocida.bounces.google.com> Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by sourceware.org (Postfix) with ESMTPS id 2A764383E83C for ; Mon, 4 May 2020 16:45:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2A764383E83C Received: by mail-qt1-x84a.google.com with SMTP id g8so176825qtq.2 for ; Mon, 04 May 2020 09:45:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Fbk4Za4hnjF0rN1KbhItdvFf7/YGgdvvaQ1SxFQQAZQ=; b=kIspSUq8Pt9pYHxTSLMtiaW71l0JbnQeRBkJ30a51F58koYVYti+l1kcmLAybBTfpk ot8XsfZf3Bnq1YUVah3OeyM08pNK81AHI3M4xMkOZUjF2TqS1++BxdXTu98rcwzyhxX7 CupwocSvTS3LZIjLFYkfg9opSmhIHlv8AmcZlc/l354OZlZT6nkANVgub54XUw4rQRn6 WeTd+TAzv3r4GbVs4CtboztZdHHsJ3ebKj8NL69XfMAt9rE3xovCiSrkPTz0wRB/fJz5 DU9DFeeElLaTC3w673XRyeQVWH2SvMECkHY8mSBTUu0eAp+80w4FdFmnDoCSNpw5dOrQ LhMw== X-Gm-Message-State: AGi0PuZ5aSNEn06bUh53Jh90jBG1Uygvg2EozCm58SsXdWIUue8f0WN5 /On1cUm4Hz4ujuzU3fwhcqDCUWcGTCqUBIwThVcWfcLaSGdo3RyhiJ+S+lPI+5xm4LFN8puSfby 0n7+n+WmbVMcQ7Gd+bczMwMijjvS3LSTSDFN/qylN6DGajmcOuuS1T5e++YMTqF13bk5gXJM= X-Google-Smtp-Source: APiQypIQcuYiZno99/3Fe59Hr7+buFX+M/oZWOfnWYMRKedLs1uyLBxE84GUyOSHZQF00bCq/e+nt0ZVEUFXXA== X-Received: by 2002:a0c:bec4:: with SMTP id f4mr17530qvj.26.1588610722683; Mon, 04 May 2020 09:45:22 -0700 (PDT) Date: Mon, 4 May 2020 17:45:20 +0100 In-Reply-To: <20200504125749.GC43931@google.com> Message-Id: <20200504164520.93024-1-gprocida@google.com> Mime-Version: 1.0 References: <20200504125749.GC43931@google.com> X-Mailer: git-send-email 2.26.2.526.g744177e7f7-goog Subject: [PATCH v5 03/15] Escape names used in symbol whitelisting regex. From: Giuliano Procida To: libabigail@sourceware.org Cc: dodji@seketeli.org, kernel-team@android.com, gprocida@google.com, Matthias Maennich Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-25.7 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libabigail mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 May 2020 16:45:27 -0000 There is the theoretical possibility that symbols may contain special regex characters like '.' and '$'. This patch ensures all such characters in symbol names are escaped before they are added to the whitelisting regex. * include/regex.h (escape): New string reference holder class. (operator<<): Declaration of std::ostream, regex::escape overload. * include/regex.cc (operator<<): New std::ostream, regex::escape overload that outputs regex-escaped strings. * src/abg-tools-utils.cc (gen_suppr_spec_from_kernel_abi_whitelists): Make sure any special regex characters in symbol names are escaped. Reviewed-by: Matthias Maennich Signed-off-by: Giuliano Procida --- include/abg-regex.h | 10 ++++++++++ src/abg-regex.cc | 28 ++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/abg-regex.h b/include/abg-regex.h index d39ada46..164083cc 100644 --- a/include/abg-regex.h +++ b/include/abg-regex.h @@ -58,6 +58,16 @@ struct regex_t_deleter } };//end struct regex_deleter +/// A class to hold a reference to a string to regex escape. +struct escape +{ + escape(const std::string& str) : ref(str) { } + const std::string& ref; +}; + +std::ostream& +operator<<(std::ostream& os, const escape& esc); + std::string generate_from_strings(const std::vector& strs); diff --git a/src/abg-regex.cc b/src/abg-regex.cc index 0451d111..59f56deb 100644 --- a/src/abg-regex.cc +++ b/src/abg-regex.cc @@ -24,6 +24,7 @@ /// #include +#include #include "abg-regex.h" #include "abg-sptr-utils.h" @@ -57,6 +58,29 @@ sptr_utils::build_sptr() namespace regex { +/// Escape regex special charaters in input string. +/// +/// @param os the output stream being written to. +/// +/// @param esc the regex_escape object holding a reference to the string +/// needing to be escaped. +/// +/// @return the output stream. +std::ostream& +operator<<(std::ostream& os, const escape& esc) +{ + // ']' and '}' are only conditionally special, so could be removed. + static const std::string specials = "^.[]$()|*+?{}\\"; + const std::string& str = esc.ref; + for (std::string::const_iterator i = str.begin(); i != str.end(); ++i) + { + if (specials.find(*i) != std::string::npos) + os << '\\'; + os << *i; + } + return os; +} + /// Generate a regex pattern equivalent to testing set membership. /// /// A string will match the resulting pattern regex, if and only if it @@ -73,9 +97,9 @@ generate_from_strings(const std::vector& strs) return "^_^"; std::ostringstream os; std::vector::const_iterator i = strs.begin(); - os << "^(" << *i++; + os << "^(" << escape(*i++); while (i != strs.end()) - os << "|" << *i++; + os << "|" << escape(*i++); os << ")$"; return os.str(); } -- 2.26.2.526.g744177e7f7-goog