]> git.pld-linux.org Git - packages/libcgroup.git/blob - libcgroup-0.41-size-of-controller-values.patch
08aba87acf04c1d649d821155a17f9d5d40deb3c
[packages/libcgroup.git] / libcgroup-0.41-size-of-controller-values.patch
1 From 5a64a79144e58a62426a34ef51b14e891f042fa2 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Nikola=20Forr=C3=B3?= <nforro@redhat.com>
3 Date: Tue, 17 Apr 2018 13:54:38 +0200
4 Subject: [PATCH 6/6] Increase maximal size of controller values
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Maximal length of a controller value is determined by CG_VALUE_MAX,
10 which is equal to 100. That is not sufficient in some cases.
11
12 Add new constant CG_CONTROL_VALUE_MAX (to prevent breaking current API)
13 and set it to 4096, which is usually equal to the amount of bytes that
14 can be written to a sysctl file directly.
15
16 Add warning message about exceeding the limit while parsing
17 configuration file.
18
19 Signed-off-by: Nikola Forró <nforro@redhat.com>
20 ---
21  src/api.c                |  6 +++---
22  src/libcgroup-internal.h |  5 ++++-
23  src/tools/cgset.c        |  4 ++--
24  src/wrapper.c            | 17 ++++++++++++-----
25  4 files changed, 21 insertions(+), 11 deletions(-)
26
27 diff --git a/src/api.c b/src/api.c
28 index efde2d1..1cd30df 100644
29 --- a/src/api.c
30 +++ b/src/api.c
31 @@ -1561,7 +1561,7 @@ static int cgroup_copy_controller_values(struct cgroup_controller *dst,
32                 }
33  
34                 dst_val = dst->values[i];
35 -               strncpy(dst_val->value, src_val->value, CG_VALUE_MAX);
36 +               strncpy(dst_val->value, src_val->value, CG_CONTROL_VALUE_MAX);
37                 strncpy(dst_val->name, src_val->name, FILENAME_MAX);
38                 dst_val->dirty = src_val->dirty;
39         }
40 @@ -2286,7 +2286,7 @@ static int cg_rd_ctrl_file(const char *subsys, const char *cgroup,
41         if (ctrl_file < 0)
42                 return ECGROUPVALUENOTEXIST;
43  
44 -       *value = calloc(CG_VALUE_MAX, 1);
45 +       *value = calloc(CG_CONTROL_VALUE_MAX, 1);
46         if (!*value) {
47                 close(ctrl_file);
48                 last_errno = errno;
49 @@ -2297,7 +2297,7 @@ static int cg_rd_ctrl_file(const char *subsys, const char *cgroup,
50          * using %as or fread crashes when we try to read from files like
51          * memory.stat
52          */
53 -       ret = read(ctrl_file, *value, CG_VALUE_MAX-1);
54 +       ret = read(ctrl_file, *value, CG_CONTROL_VALUE_MAX-1);
55         if (ret < 0) {
56                 free(*value);
57                 *value = NULL;
58 diff --git a/src/libcgroup-internal.h b/src/libcgroup-internal.h
59 index 4c0f46c..3a8e336 100644
60 --- a/src/libcgroup-internal.h
61 +++ b/src/libcgroup-internal.h
62 @@ -32,6 +32,9 @@ __BEGIN_DECLS
63  /* Estimated number of groups created */
64  #define MAX_GROUP_ELEMENTS     128
65  
66 +/* Maximum length of a value */
67 +#define CG_CONTROL_VALUE_MAX 4096
68 +
69  #define CG_NV_MAX 100
70  #define CG_CONTROLLER_MAX 100
71  /* Max number of mounted hierarchies. Event if one controller is mounted per
72 @@ -73,7 +76,7 @@ __BEGIN_DECLS
73  
74  struct control_value {
75         char name[FILENAME_MAX];
76 -       char value[CG_VALUE_MAX];
77 +       char value[CG_CONTROL_VALUE_MAX];
78         bool dirty;
79  };
80  
81 diff --git a/src/tools/cgset.c b/src/tools/cgset.c
82 index ea9f90d..3d3c8cc 100644
83 --- a/src/tools/cgset.c
84 +++ b/src/tools/cgset.c
85 @@ -151,8 +151,8 @@ int main(int argc, char *argv[])
86                                 goto err;
87                         }
88  
89 -                       strncpy(name_value[nv_number].value, buf, CG_VALUE_MAX);
90 -                       name_value[nv_number].value[CG_VALUE_MAX-1] = '\0';
91 +                       strncpy(name_value[nv_number].value, buf, CG_CONTROL_VALUE_MAX);
92 +                       name_value[nv_number].value[CG_CONTROL_VALUE_MAX-1] = '\0';
93  
94                         nv_number++;
95                         break;
96 diff --git a/src/wrapper.c b/src/wrapper.c
97 index c03472a..0952823 100644
98 --- a/src/wrapper.c
99 +++ b/src/wrapper.c
100 @@ -132,10 +132,10 @@ int cgroup_add_value_string(struct cgroup_controller *controller,
101         if (!controller)
102                 return ECGINVAL;
103  
104 -       if (controller->index >= CG_VALUE_MAX)
105 +       if (controller->index >= CG_NV_MAX)
106                 return ECGMAXVALUESEXCEEDED;
107  
108 -       for (i = 0; i < controller->index && i < CG_VALUE_MAX; i++) {
109 +       for (i = 0; i < controller->index && i < CG_NV_MAX; i++) {
110                 if (!strcmp(controller->values[i]->name, name))
111                         return ECGVALUEEXISTS;
112         }
113 @@ -145,8 +145,15 @@ int cgroup_add_value_string(struct cgroup_controller *controller,
114         if (!cntl_value)
115                 return ECGCONTROLLERCREATEFAILED;
116  
117 -       strncpy(cntl_value->name, name, sizeof(cntl_value->name));
118 -       strncpy(cntl_value->value, value, sizeof(cntl_value->value));
119 +       if (strlen(value) >= sizeof(cntl_value->value)) {
120 +               fprintf(stderr, "value exceeds the maximum of %d characters\n",
121 +                       sizeof(cntl_value->value));
122 +               free(cntl_value);
123 +               return ECGCONFIGPARSEFAIL;
124 +       }
125 +
126 +       strncpy(cntl_value->name, name, sizeof(cntl_value->name) - 1);
127 +       strncpy(cntl_value->value, value, sizeof(cntl_value->value) - 1);
128         cntl_value->dirty = true;
129         controller->values[controller->index] = cntl_value;
130         controller->index++;
131 @@ -356,7 +363,7 @@ int cgroup_set_value_string(struct cgroup_controller *controller,
132         for (i = 0; i < controller->index; i++) {
133                 struct control_value *val = controller->values[i];
134                 if (!strcmp(val->name, name)) {
135 -                       strncpy(val->value, value, CG_VALUE_MAX);
136 +                       strncpy(val->value, value, CG_CONTROL_VALUE_MAX - 1);
137                         val->dirty = true;
138                         return 0;
139                 }
140 -- 
141 2.17.0
142
This page took 0.089474 seconds and 2 git commands to generate.