mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: ebiederm@xmission.com (Eric W. Biederman)
To: Andrew Morton <akpm@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, devel@openvz.org,
	Alexey Dobriyan <adobriyan@sw.ru>
Subject: [PATCH 2/3] sysctl: Factor out sysctl_data.
Date: Thu, 09 Aug 2007 13:52:16 -0600	[thread overview]
Message-ID: <m1ps1wa3kf.fsf_-_@ebiederm.dsl.xmission.com> (raw)
In-Reply-To: <m1tzr8a3ms.fsf_-_@ebiederm.dsl.xmission.com> (Eric W. Biederman's message of "Thu, 09 Aug 2007 13:50:51 -0600")


There as been no easy way to wrap the default sysctl strategy routine
except for returning 0.  Which is not always what we want.  The few
instances I have seen that want different behaviour have written their own
version of sysctl_data.  While not too hard it is unnecessary code and
has the potential for extra bugs.

So to make these situnations easier and make that part of sysctl
more symetric I have factord sysctl_data out of do_sysctl_strategy
and exported as a function everyone can use.

Further having sysctl_data be an explicit function makes checking
for badly formed sysctl tables much easier.

Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
---
 include/linux/sysctl.h |    1 +
 kernel/sysctl.c        |   66 +++++++++++++++++++++++++++++++++--------------
 2 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/include/linux/sysctl.h b/include/linux/sysctl.h
index f73be4c..5ca510b 100644
--- a/include/linux/sysctl.h
+++ b/include/linux/sysctl.h
@@ -972,6 +972,7 @@ extern int do_sysctl_strategy (struct ctl_table *table,
 			       void __user *oldval, size_t __user *oldlenp,
 			       void __user *newval, size_t newlen);
 
+extern ctl_handler sysctl_data;
 extern ctl_handler sysctl_string;
 extern ctl_handler sysctl_intvec;
 extern ctl_handler sysctl_jiffies;
diff --git a/kernel/sysctl.c b/kernel/sysctl.c
index 6723f92..cf4c632 100644
--- a/kernel/sysctl.c
+++ b/kernel/sysctl.c
@@ -1406,7 +1406,6 @@ int do_sysctl_strategy (struct ctl_table *table,
 			void __user *newval, size_t newlen)
 {
 	int op = 0, rc;
-	size_t len;
 
 	if (oldval)
 		op |= 004;
@@ -1427,25 +1426,10 @@ int do_sysctl_strategy (struct ctl_table *table,
 	/* If there is no strategy routine, or if the strategy returns
 	 * zero, proceed with automatic r/w */
 	if (table->data && table->maxlen) {
-		if (oldval && oldlenp) {
-			if (get_user(len, oldlenp))
-				return -EFAULT;
-			if (len) {
-				if (len > table->maxlen)
-					len = table->maxlen;
-				if(copy_to_user(oldval, table->data, len))
-					return -EFAULT;
-				if(put_user(len, oldlenp))
-					return -EFAULT;
-			}
-		}
-		if (newval && newlen) {
-			len = newlen;
-			if (len > table->maxlen)
-				len = table->maxlen;
-			if(copy_from_user(table->data, newval, len))
-				return -EFAULT;
-		}
+		rc = sysctl_data(table, name, nlen, oldval, oldlenp,
+				 newval, newlen);
+		if (rc < 0)
+			return rc;
 	}
 	return 0;
 }
@@ -2344,6 +2328,40 @@ int proc_doulongvec_ms_jiffies_minmax(struct ctl_table *table, int write,
  * General sysctl support routines 
  */
 
+/* The generic sysctl data routine (used if no strategy routine supplied) */
+int sysctl_data(struct ctl_table *table, int __user *name, int nlen,
+		void __user *oldval, size_t __user *oldlenp,
+		void __user *newval, size_t newlen)
+{
+	size_t len;
+
+	/* Get out of I don't have a variable */
+	if (!table->data || !table->maxlen)
+		return -ENOTDIR;
+
+	if (oldval && oldlenp) {
+		if (get_user(len, oldlenp))
+			return -EFAULT;
+		if (len) {
+			if (len > table->maxlen)
+				len = table->maxlen;
+			if (copy_to_user(oldval, table->data, len))
+				return -EFAULT;
+			if (put_user(len, oldlenp))
+				return -EFAULT;
+		}
+	}
+
+	if (newval && newlen) {
+		if (newlen > table->maxlen)
+			newlen = table->maxlen;
+
+		if (copy_from_user(table->data, newval, newlen))
+			return -EFAULT;
+	}
+	return 1;
+}
+
 /* The generic string strategy routine: */
 int sysctl_string(struct ctl_table *table, int __user *name, int nlen,
 		  void __user *oldval, size_t __user *oldlenp,
@@ -2532,6 +2550,13 @@ out:
 	return -ENOSYS;
 }
 
+int sysctl_data(struct ctl_table *table, int __user *name, int nlen,
+		  void __user *oldval, size_t __user *oldlenp,
+		  void __user *newval, size_t newlen)
+{
+	return -ENOSYS;
+}
+
 int sysctl_string(struct ctl_table *table, int __user *name, int nlen,
 		  void __user *oldval, size_t __user *oldlenp,
 		  void __user *newval, size_t newlen)
@@ -2579,4 +2604,5 @@ EXPORT_SYMBOL(sysctl_intvec);
 EXPORT_SYMBOL(sysctl_jiffies);
 EXPORT_SYMBOL(sysctl_ms_jiffies);
 EXPORT_SYMBOL(sysctl_string);
+EXPORT_SYMBOL(sysctl_data);
 EXPORT_SYMBOL(unregister_sysctl_table);
-- 
1.5.1.1.181.g2de0


  reply	other threads:[~2007-08-09 19:53 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-26 16:45 [PATCH] Remove CTL_UNNUMBERED Alexey Dobriyan
2007-07-26 17:24 ` Eric W. Biederman
2007-07-27 14:52   ` Alexey Dobriyan
2007-08-06 12:45     ` [PATCH 2/2] sysctl: remove CTL_UNNUMBERED Alexey Dobriyan
2007-08-09 19:44       ` Eric W. Biederman
2007-08-09 19:50       ` [PATCH 1/3] sysctl core: Stop using the unnecessary ctl_table typedef Eric W. Biederman
2007-08-09 19:52         ` Eric W. Biederman [this message]
2007-08-09 20:09           ` [PATCH 3/3] sysctl: Error on bad sysctl tables Eric W. Biederman
2007-08-10  0:49             ` [PATCH 01/10] sysctl: Update sysctl_check_table Eric W. Biederman
2007-08-10  0:51               ` [PATCH 02/10] sysct mqueue: Remove the binary sysctl numbers Eric W. Biederman
2007-08-10  0:53                 ` [PATCH 03/10] sysctl: Remove binary sysctl support where it clearly doesn't work Eric W. Biederman
2007-08-10  0:56                   ` [PATCH 04/10] sysctl: Fix neighbour table sysctls Eric W. Biederman
2007-08-10  0:57                     ` [PATCH 05/10] sysctl: ipv6 route flushing (kill binary path) Eric W. Biederman
2007-08-10  0:58                       ` [PATCH 06/10] sysctl: Remove broken sunrpc debug binary sysctls Eric W. Biederman
2007-08-10  0:59                         ` [PATCH 07/10] sysctl: x86_64 remove unnecessary binary paths Eric W. Biederman
2007-08-10  1:01                           ` [PATCH 08/10] sysctl: Remove broken cdrom binary sysctls Eric W. Biederman
2007-08-10  1:02                             ` [PATCH 09/10] sysctl: ipv4 remove binary sysctl paths where they are broken Eric W. Biederman
2007-08-10  1:03                               ` [PATCH 10/10] sysctl: Remove the binary interface for aio-nr, aio-max-nr, acpi_video_flags Eric W. Biederman
2007-08-10 13:33                             ` [PATCH 08/10] sysctl: Remove broken cdrom binary sysctls Alan Cox
2007-08-10 15:55                               ` Eric W. Biederman
2007-08-10 17:16                                 ` Alan Cox
2007-08-10 18:30                                   ` Eric W. Biederman
2007-08-10  1:04                       ` [PATCH 06/10] sysctl: Remove broken sunrpc debug " Eric W. Biederman
2007-08-10  1:47                     ` [PATCH 04/10] sysctl: Fix neighbour table sysctls YOSHIFUJI Hideaki / 吉藤英明
2007-08-10  1:49                       ` David Miller
2007-08-10  2:14                         ` YOSHIFUJI Hideaki / 吉藤英明
2007-08-10  2:23                           ` Eric W. Biederman
2007-08-10  2:29                             ` YOSHIFUJI Hideaki / 吉藤英明
2007-08-10  2:35                               ` Eric W. Biederman
2007-08-10  1:55                       ` Andrew Morton
2007-08-10  2:12                         ` Eric W. Biederman
2007-08-10  2:22                     ` YOSHIFUJI Hideaki / 吉藤英明
2007-08-10  2:01             ` [PATCH 3/3] sysctl: Error on bad sysctl tables YOSHIFUJI Hideaki / 吉藤英明
2007-08-10  2:15               ` Eric W. Biederman
2007-08-10  2:18               ` Eric W. Biederman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m1ps1wa3kf.fsf_-_@ebiederm.dsl.xmission.com \
    --to=ebiederm@xmission.com \
    --cc=adobriyan@sw.ru \
    --cc=akpm@linux-foundation.org \
    --cc=devel@openvz.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome