From patchwork Wed Apr 16 12:55:18 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Layton X-Patchwork-Id: 4000851 Return-Path: X-Original-To: patchwork-cifs-client@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 3B683BFF02 for ; Wed, 16 Apr 2014 12:55:31 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 5A7A220170 for ; Wed, 16 Apr 2014 12:55:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 50471201B4 for ; Wed, 16 Apr 2014 12:55:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755512AbaDPMz2 (ORCPT ); Wed, 16 Apr 2014 08:55:28 -0400 Received: from mail-qg0-f45.google.com ([209.85.192.45]:63886 "EHLO mail-qg0-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755471AbaDPMz1 (ORCPT ); Wed, 16 Apr 2014 08:55:27 -0400 Received: by mail-qg0-f45.google.com with SMTP id j5so11112478qga.18 for ; Wed, 16 Apr 2014 05:55:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=zgupbeU7ooVchiCftkRudBy74+MBqLJBExeBA5oS5iQ=; b=bid3/Ye5e6ivVt+PcgtVmszvhVmnkb+oghPDjoqJQbBSSCE8Ba8u4tB0ilegT1xzuE oTbFm6h/bQKQViTlyotz9gSP6bVFCtInDp5av8K/oVQvENgJNNeaM+u9c0S8J+c4ajkK z1GjcBLzhhGwnyMY6QKr7eDwVHSVYqK3IFqADD9aBdigvLJdIuQIzAxfpl6xUy2bb4wu 6Z6/811N0BZ9gSTAY1513NqpAdkHrMd03xR6db+mRF0IwuGf7qDFn+7s1QFi7iQGiM0N btSwwrzye9iEj2n5UfDS+y0KFvWvLKyg9DcLwFnMwAZJ4bqfs81K8+33YxYDbuUTBhq0 WVWQ== X-Gm-Message-State: ALoCoQnMF7JBqcZmktOuN5Vbxru3iGDWW43kJpqI39I85c2UQu4zgfU44+9/HRgAyOyHhvHvRqEF X-Received: by 10.224.96.134 with SMTP id h6mr2115152qan.100.1397652926965; Wed, 16 Apr 2014 05:55:26 -0700 (PDT) Received: from tlielax.poochiereds.net ([2001:470:8:d63:3a60:77ff:fe93:a95d]) by mx.google.com with ESMTPSA id s15sm4878326qac.37.2014.04.16.05.55.26 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 16 Apr 2014 05:55:26 -0700 (PDT) From: Jeff Layton To: linux-cifs@vger.kernel.org Cc: Sebastian Krahmer Subject: [cifs-utils PATCH 2/2] cifscreds: better error handling for key_add Date: Wed, 16 Apr 2014 08:55:18 -0400 Message-Id: <1397652918-14002-2-git-send-email-jlayton@samba.org> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1397652918-14002-1-git-send-email-jlayton@samba.org> References: <1397652918-14002-1-git-send-email-jlayton@samba.org> Sender: linux-cifs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-cifs@vger.kernel.org X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If the string buffers would have been overrun, set errno to EINVAL before returning. Then, have the callers report the errors to stderr or syslog as appropriate. Cc: Sebastian Krahmer Signed-off-by: Jeff Layton --- cifscreds.c | 6 +++--- cifskey.c | 8 ++++++-- pam_cifscreds.c | 8 ++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cifscreds.c b/cifscreds.c index 64d55b0cac0e..5d84c3c87873 100644 --- a/cifscreds.c +++ b/cifscreds.c @@ -220,8 +220,8 @@ static int cifscreds_add(struct cmdarg *arg) while (currentaddress) { key_serial_t key = key_add(currentaddress, arg->user, pass, arg->keytype); if (key <= 0) { - fprintf(stderr, "error: Add credential key for %s\n", - currentaddress); + fprintf(stderr, "error: Add credential key for %s: %s\n", + currentaddress, strerror(errno)); } else { if (keyctl(KEYCTL_SETPERM, key, CIFS_KEY_PERMS) < 0) { fprintf(stderr, "error: Setting permissons " @@ -422,7 +422,7 @@ static int cifscreds_update(struct cmdarg *arg) key_serial_t key = key_add(addrs[id], arg->user, pass, arg->keytype); if (key <= 0) fprintf(stderr, "error: Update credential key " - "for %s\n", addrs[id]); + "for %s: %s\n", addrs[id], strerror(errno)); } return EXIT_SUCCESS; diff --git a/cifskey.c b/cifskey.c index 4f01ed0e10bd..919540f549ad 100644 --- a/cifskey.c +++ b/cifskey.c @@ -47,13 +47,17 @@ key_add(const char *addr, const char *user, const char *pass, char keytype) char val[MOUNT_PASSWD_SIZE + MAX_USERNAME_SIZE + 2]; /* set key description */ - if (snprintf(desc, sizeof(desc), "%s:%c:%s", KEY_PREFIX, keytype, addr) >= (int)sizeof(desc)) + if (snprintf(desc, sizeof(desc), "%s:%c:%s", KEY_PREFIX, keytype, addr) >= (int)sizeof(desc)) { + errno = EINVAL; return -1; + } /* set payload contents */ len = snprintf(val, sizeof(val), "%s:%s", user, pass); - if (len >= (int)sizeof(val)) + if (len >= (int)sizeof(val)) { + errno = EINVAL; return -1; + } return add_key(CIFS_KEY_TYPE, desc, val, len + 1, DEST_KEYRING); } diff --git a/pam_cifscreds.c b/pam_cifscreds.c index fb23117953f0..3459105045b2 100644 --- a/pam_cifscreds.c +++ b/pam_cifscreds.c @@ -233,8 +233,8 @@ static int cifscreds_pam_add(pam_handle_t *ph, const char *user, const char *pas while (currentaddress) { key_serial_t key = key_add(currentaddress, user, password, keytype); if (key <= 0) { - pam_syslog(ph, LOG_ERR, "error: Add credential key for %s", - currentaddress); + pam_syslog(ph, LOG_ERR, "error: Add credential key for %s: %s", + currentaddress, strerror(errno)); } else { if ((args & ARG_DEBUG) == ARG_DEBUG) { pam_syslog(ph, LOG_DEBUG, "credential key for \\\\%s\\%s added", @@ -336,8 +336,8 @@ static int cifscreds_pam_update(pam_handle_t *ph, const char *user, const char * for (id = 0; id < count; id++) { key_serial_t key = key_add(currentaddress, user, password, keytype); if (key <= 0) { - pam_syslog(ph, LOG_ERR, "error: Update credential key for %s", - currentaddress); + pam_syslog(ph, LOG_ERR, "error: Update credential key for %s: %s", + currentaddress, strerror(errno)); } }